clickedButtonAtIndex in appdelegate is not called

荒凉一梦 提交于 2019-12-22 08:03:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!