[iPhone]UIDatePickerのメモ(自分用)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
CGRect frameForWindow = [[UIScreen mainScreen] bounds];
_window = [[UIWindow alloc] initWithFrame:frameForWindow];
//アプリのウインドウと同じサイズの四角形を取得
CGRect rect = CGRectMake(0.0, 20.0, 320.0, 460.0);
//上記で取得したサイズのピッカーを作成
picker_ = [[UIDatePicker alloc] initWithFrame:rect];
NSDate *date = [NSDate date];
[picker_ setDate:date animated:NO];
[picker_ setMinimumDate:date];
[picker_ setMaximumDate:[NSDate dateWithTimeIntervalSinceNow:3600*24*365]];
[picker_ setCalendar:[NSCalendar currentCalendar]];
[picker_ setTimeZone:[NSTimeZone systemTimeZone]];
[_window addSubview:picker_];
// [picker release];
// 標準ボタン例文
btn_ = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_.frame = CGRectMake(100, 300, 100, 30);
[btn_ setTitle:@"押してね" forState:UIControlStateNormal];
[btn_ setTitle:@"ぽち" forState:UIControlStateHighlighted];
[btn_ setTitle:@"押せません" forState:UIControlStateDisabled];
// ボタンがタッチダウンされた時にhogeメソッドを呼び出す
[btn_ addTarget:self action:@selector(pushBtn:)forControlEvents:UIControlEventTouchDown];
[_window addSubview:btn_];
// [btn release];
[_window makeKeyAndVisible];
return YES;
}
- (void)pushBtn:(UIButton*)button{
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *intputDateStr = @"2012/09/02 03:04:05";
[inputDateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[inputDateFormatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
NSDate *inputDate = [inputDateFormatter dateFromString:intputDateStr];
[inputDateFormatter release];
[picker_ setDate:inputDate animated:YES];
NSLog(@"picker_ => %@",[picker_ description]);
NSLog(@"picker_.date => %@",[picker_.date description]);
NSLog(@"picker_.minimumDate => %@",[picker_.minimumDate description]);
NSLog(@"picker_.maximumDate => %@",[picker_.maximumDate description]);
NSLog(@"picker_.calendar => %@",[picker_.calendar description]);
NSLog(@"picker_.timeZone => %@",[picker_.timeZone description]);
}
UIDataPickerのdateは本体のカレンダー設定にどうしても引きずられるようです。
それはどうしようもなさそう。。
設定するdateはフォマッタで文字列の与える文字列のフォーマットを
カレンダーとフォーマットストリングで指定してあげればよい。
{
CGRect frameForWindow = [[UIScreen mainScreen] bounds];
_window = [[UIWindow alloc] initWithFrame:frameForWindow];
//アプリのウインドウと同じサイズの四角形を取得
CGRect rect = CGRectMake(0.0, 20.0, 320.0, 460.0);
//上記で取得したサイズのピッカーを作成
picker_ = [[UIDatePicker alloc] initWithFrame:rect];
NSDate *date = [NSDate date];
[picker_ setDate:date animated:NO];
[picker_ setMinimumDate:date];
[picker_ setMaximumDate:[NSDate dateWithTimeIntervalSinceNow:3600*24*365]];
[picker_ setCalendar:[NSCalendar currentCalendar]];
[picker_ setTimeZone:[NSTimeZone systemTimeZone]];
[_window addSubview:picker_];
// [picker release];
// 標準ボタン例文
btn_ = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_.frame = CGRectMake(100, 300, 100, 30);
[btn_ setTitle:@"押してね" forState:UIControlStateNormal];
[btn_ setTitle:@"ぽち" forState:UIControlStateHighlighted];
[btn_ setTitle:@"押せません" forState:UIControlStateDisabled];
// ボタンがタッチダウンされた時にhogeメソッドを呼び出す
[btn_ addTarget:self action:@selector(pushBtn:)forControlEvents:UIControlEventTouchDown];
[_window addSubview:btn_];
// [btn release];
[_window makeKeyAndVisible];
return YES;
}
- (void)pushBtn:(UIButton*)button{
NSDateFormatter *inputDateFormatter = [[NSDateFormatter alloc] init];
[inputDateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *intputDateStr = @"2012/09/02 03:04:05";
[inputDateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[inputDateFormatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]];
NSDate *inputDate = [inputDateFormatter dateFromString:intputDateStr];
[inputDateFormatter release];
[picker_ setDate:inputDate animated:YES];
NSLog(@"picker_ => %@",[picker_ description]);
NSLog(@"picker_.date => %@",[picker_.date description]);
NSLog(@"picker_.minimumDate => %@",[picker_.minimumDate description]);
NSLog(@"picker_.maximumDate => %@",[picker_.maximumDate description]);
NSLog(@"picker_.calendar => %@",[picker_.calendar description]);
NSLog(@"picker_.timeZone => %@",[picker_.timeZone description]);
}
UIDataPickerのdateは本体のカレンダー設定にどうしても引きずられるようです。
それはどうしようもなさそう。。
設定するdateはフォマッタで文字列の与える文字列のフォーマットを
カレンダーとフォーマットストリングで指定してあげればよい。
スポンサーサイト