標籤:style blog http io ar color os sp for
在做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:string27 delegate:self28 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,其他按鈕序號按添加次序遞增。
iOS 8 & Xcode 6:UIAlertView取消時不回調alertViewCancel: