1 #import "AppDelegate.h" 2 3 @interface AppDelegate () 4 @property (nonatomic,strong)UILabel *label; 5 @end 6 7 @implementation AppDelegate 8 9 - (void)dealloc 10 { 11 self.label = nil; 12 self.window = nil; 13 } 14 15 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 16 self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 17 // Override point for customization after application launch. 18 self.window.backgroundColor = [UIColor whiteColor]; 19 20 UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectZero]; 21 picker.datePickerMode = UIDatePickerModeDateAndTime; 22 // 添加事件 23 [picker addTarget:self action:@selector(changeDateOfPicker:) forControlEvents:UIControlEventValueChanged]; 24 [self.window addSubview:picker]; 25 // 创建label 26 self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, [UIScreen mainScreen].bounds.size.width, 100)]; 27 self.label.backgroundColor = [UIColor clearColor]; 28 self.label.textAlignment = NSTextAlignmentCenter; 29 [self.window addSubview:self.label]; 30 31 [self.window makeKeyAndVisible]; 32 return YES; 33 } 34 35 - (void)changeDateOfPicker:(UIDatePicker *)sender 36 { 37 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 38 [dateFormatter setDateFormat:@"Y/MM/dd H:mm:ss"]; 39 NSString *dateString = [dateFormatter stringFromDate:[sender date]]; 40 41 self.label.text = dateString; 42 } 43 44 45 @end