开发者

alertView didDismissWithButtomIndex is not called after a click on the alertView's buttom

I have recently begun my studies on iOS development so forgive me if I am asking something too obvious.

When my application's view loads it checks the configurations for some keys and if there's no value for these keys the application should display an alert and quit.

First of all, I have implemented the UIAlertViewDelegate:

@interface FirstViewController : UIViewController <UIAlertViewDelegate> {
...

And then checked for the settings:

    - (void)viewDidLoad {
          NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

              NSString *url = [defaults stringForKey:@"url"];
          NSString *apiKey = [defaults stringForKey:@"api-key"];

          if([url length] < 1 || [apiKey length] < 1){
           UIAlertView *dialog = [[[UIAlertView alloc] 
                       initWithTitle:@"Not properly configured" 
                       message:@"This application was not properly configured. Please configure the application on your iPhone settings." 
               delegate:self 
               cancelButtonTitle:@"Close"
               otherButtonTitles:nil]
               autorelease];
        [dialog setTag:1];
        [dialog show];
    }

    [url release];
    [apiKey release];
    [super viewDidLoad];
}

I understand that the method alertView didDismissWithButtomIndex should be called after the alertView's dismiss but for some reason this method is never called in my code.

    - (void)alertView:(UIAlertView *)alertView                   didDismissWithButtomIndex:(NSInteger)buttomIndex {
               if([alertView tag] == 1)开发者_开发百科{
                exit(0);
               }
}

Any ideas on why this is happening?


didDismissWithButtonIndex is misspelled, you snuck an ‘m’ in there instead of ’n’.


You are listening for the wrong method, you should implement :

alertView:clickedButtonAtIndex:

In the doc you can read that the didDismissWithButtomIndex is called when dismissWithClickedButtonIndex:animated: is called on the alertView.

alertView:didDismissWithButtonIndex: Sent to the delegate after an alert view is dismissed from the screen.

So for your code to work you should implementsomething like :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
  if(buttonIndex == ...) {
    // do something
  }
}

PS: You shouldn't call exit(0), it is a bad practice on iOS to force an application to quit. User are supposed to quit the app with the home button.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜