iOS 8 & Xcode 6:UIAlertView取消时不回调alertViewCancel:
在做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,其他按钮序号按添加次序递增。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。