在做UIAlertView取消操作时,发现委托函数alertViewCancel:并不被调用,只好在alertView: didDismissWithButtonIndex:中进行判断。
1 @interface DateViewController () <UIAlertViewDelegate> 2 @property (weak, nonatomic) IBOutlet UIDatePicker *datePicker; 3 4 @end 5 6 @implementation DateViewController 7 8 - (void)viewDidLoad { 9 [super viewDidLoad]; 10 NSDate *now = [NSDate date]; 11 [self.datePicker setDate:now animated:NO]; 12 } 13 14 - (void)didReceiveMemoryWarning { 15 [super didReceiveMemoryWarning]; 16 // Dispose of any resources that can be recreated. 17 } 18 19 - (IBAction)buttonPress:(UIButton *)sender { 20 NSDate *selectedDate = self.datePicker.date; 21 NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; 22 fmt.dateFormat = @"yyyy - MM - dd"; 23 24 NSString *string = [NSString stringWithFormat:@"You selecte %@", [fmt stringFromDate:selectedDate]]; 25 UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"Date Picker" 26 message:string 27 delegate:self 28 cancelButtonTitle:@"Cancel" 29 otherButtonTitles:@"OK", nil]; 30 [view show]; 31 } 32 33 - (void)alertViewCancel:(UIAlertView *)alertView { 34 NSLog(@"View canceled."); 35 } 36 37 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 38 NSLog(@"buttonIndex = %zi", buttonIndex); 39 } 40 41 @end
日志输出如下,取消按钮序号为0,其他按钮序号按添加次序递增。
时间: 2024-11-13 10:31:23