When doing Uialertview cancel operation, find the delegate function Alertviewcancel: not be called, have to judge in Alertview:diddismisswithbuttonindex:.
1 @interfaceDateviewcontroller () <UIAlertViewDelegate>2@property (Weak, nonatomic) Iboutlet Uidatepicker *DatePicker;3 4 @end5 6 @implementationDateviewcontroller7 8- (void) Viewdidload {9 [Super Viewdidload];TenNSDate *now =[NSDate Date]; One [Self.datepicker Setdate:now animated:no]; A } - -- (void) didreceivememorywarning { the [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. - } - +-(Ibaction) ButtonPress: (UIButton *) Sender { -NSDate *selecteddate =self.datePicker.date; +NSDateFormatter *fmt =[[NSDateFormatter alloc] init]; AFmt.dateformat =@"YYYY-MM-DD"; at -NSString *string= [NSString stringWithFormat:@"You selecte%@", [FMT Stringfromdate:selecteddate]]; -Uialertview *view = [[Uialertview alloc] Initwithtitle:@"Date Picker" -Messagestring - Delegate: Self -Cancelbuttontitle:@"Cancel" inOtherbuttontitles:@"OK", nil]; - [view show]; to } + -- (void) Alertviewcancel: (Uialertview *) Alertview { theNSLog (@"View canceled."); * } $ Panax Notoginseng- (void) Alertview: (Uialertview *) Alertview Diddismisswithbuttonindex: (Nsinteger) Buttonindex { -NSLog (@"Buttonindex =%zi", Buttonindex); the } + A @end
The log output is as follows, the Cancel button is numbered 0, and the other button numbers increment in order of addition.
IOS 8 & Xcode 6:uialertview Cancel without callback Alertviewcancel: