IanMK2 Blog

'Xcode'에 해당되는 글 2건

  1. 2010.10.30 [iPhone] 오늘 날짜, 시간구하기
  2. 2010.10.30 [iPhone] UIPickerView 써먹기

선언은

NSCalendar *calendar = [NSCalendar currentCalendar];

unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

NSDateComponents *c = [calendar components:unitFlags fromDate:[NSDate date]]; 

 

나중에 release하는 것을 잊지말자


[c year]    

[c month] 

[c day]    

[c hour]   

[c minute]

[c second]

Posted by IanMK2
UIPickerView는 윈도우로 치자면 콤보박스와 비슷하다. 하지만 좀 더 화려하고 굴리는 맛(?)이 있다.

써먹는 방법은 여러가지가 있지만 
1. 인터페이스빌더에서 아얘 박아넣는법. 
2. 동적으로 생성해서 불러내는법.
등이 쓰인다.(액션시트에 넣는방법도 있다)

기본적으로 테이블뷰와 아주 유사하다. 델리게이트를 추가하는것을 잊지말자.

다음은 사용되는 오버라이딩함수이다.

데이터소스가져오는것
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return (NSString*)[pointAry objectAtIndex:row];
}

컴포넌트의 개수 - 데이트피커를 떠올려보라. 시간,분,초등이 있지않는가? 바로 그 항목의 수이다.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}

데이터의 개수
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [pointAry count] ;
}

피커뷰의 가로사이즈를 설정하는것
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 100;
}

피커의 값이 변경되면 호출되는 함수
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(@"%d",row);
}

위 함수 등이 사용된다.
인터페이스에서 박아넣는법을 사용한다면 위함수만 추가 해주면 될것이다.
본인은 동적으로 불러냈다가 다시 집어넣어야하기 때문에 두번째 방법으로 하였다.

UIPickerview를 생성하고 불러낼때에는 

UIPicker pickerview = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 480, 320, 0)];  
//일부러 뷰밖에서 생성한다.
pickerview.delegate = self;
pickerview.showsSelectionIndicator = YES;
[self.view addSubview:pickerview];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pickerview.transform = CGAffineTransformMakeTranslation(0, -275); //위로 쭈욱하고 올라온다.
[UIView commitAnimations];
[self pickerView:pickerview didSelectRow:4 inComponent:0];//자동으로 처음값을 설정


집어넣을땐
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
pickerview.transform = CGAffineTransformMakeTranslation(0, 275); //그냥 아래로 다시 내려주자
[UIView commitAnimations];



물론 다 사용하고 난뒤에는 뷰에서 빼주고 릴리즈해주자.


Posted by IanMK2