问题
I am calling UIAlert with 2 buttons "Cancel" and "OK" in MyapplicationAppDelegate.m file , the alert is called but on tap of "Cancel" or "OK" button
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
method is not called.
I have added UIAlertViewDelegate in the MyapplicationAppDelegate.h file as below
#import UIKit/UIKit.h
@interface MyapplicationAppDelegate: NSObject UIApplicationDelegate,UIAlertViewDelegate
{
..
}
I want to know what else is required.
回答1:
I am not sure whether its your typo while posting the question but you should call the delegate within <.. , .. >
@interface MyapplicationAppDelegate: NSObject <UIApplicationDelegate,UIAlertViewDelegate> { .. }
Here is sample code for UIAlertView
UIAlertView *myAlertView = [[UIAlertView alloc]initWithTitle:@""message:@"Your message goes here" delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[myAlertView show];
[myAlertView release];
I am not sure myAlertView.delgate = self is the right method but i do set delegate with initWithTitle
回答2:
For me,
#define appDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate)
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
message:@"Say Yes or NO?" delegate:appDelegate
cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
alertView.tag = 1;
[alertView show];
[alertView release];
do the trick!
Now its going into, -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
in AppDelegate file without adding,
@interface MyapplicationAppDelegate: NSObject <UIApplicationDelegate,UIAlertViewDelegate> { .. }
来源:https://stackoverflow.com/questions/7027105/clickedbuttonatindex-in-appdelegate-is-not-called