方法didDismissWithButtonIndex不从单例类中触发

时间:2013-08-18 01:11:38

标签: objective-c

我正在尝试处理UIAlertView的解雇事件。但是,didDismissWithButtonIndex永远不会被调用。下面是我在单例类中的代码,我在其中生成警报。有人能发现我做错了吗?

MySingleton.h

@interface BMAppUser : NSObject <UIAlertViewDelegate> {

}

+ (id)sharedInstance;

MySingleton.m

+ (id) sharedInstance {
    static BMAppUser *sharedInstance = nil;
    @synchronized(self) {
        if (sharedInstance==nil) {
            sharedInstance = [[super allocWithZone:NULL] init];
        }
    }
    return sharedInstance;
}

-(void)promptToSetLanguagePreferences {
// Create a new alert object and set initial values.
NSString *message = [NSString stringWithFormat:@"Please set your language preference settings.  Click OK to go there now."];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Language Preferences Not Set"
                                                message:message
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];
// Display the alert to the user
[alert show];
}

-(void)alertView:(UIAlertView *)didDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSLog(@"THIS METHOD NEVER GETS CALLED!!!!!!!");
    if(buttonIndex==0){
        NSLog(@"userclickedCancel");
    }
    if(buttonIndex==1){
        NSLog(@"userclickedOK");
    }
}

1 个答案:

答案 0 :(得分:1)

您实际上已声明名为alertView::而不是alertView:didDismissWithButtonIndex:的方法,因为您没有为该方法提供UIAlertView参数的名称。这在我构建时会生成编译器警告,即:

  

“'didDismissWithButtonIndex'用作前一个名称   参数而不是选择器“。

的一部分

您需要在委托方法中为UIAlertView参数提供名称。将其定义的开头更改为以下内容:

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex