开发者

Why doesn't the UIAlertView work?

I am testing this piece of code but when I run it, it does not trigger the UIAlertView. When the code hits If (ongoingGame = YES) and NSLog it jumps directly to the 'otherButtonTitles:nil' without executing the UIAlertView.

Can someone please explain to me why it does not trigger it?

    -(IBAction)continueGame_button:(id)sender {
    //=====CHECK IF THERE开发者_JS百科 IS AN ON-GOING GAME, IF SO CONTINUE=====//
    AccessCurrentGameData *isThereAnOngoingGameFunction = [AccessCurrentGameData new];
    BOOL ongoingGame = [isThereAnOngoingGameFunction checkIfGameOngoing];
    [isThereAnOngoingGameFunction release];
    NSLog(@"+ + +continueGame_button+ + +");
    NSLog(@"ongoingGame = %@\n", (ongoingGame ? @"YES" : @"NO"));

    if (ongoingGame == YES) {
        NSLog(@"++++++++++++++++++");
        NSLog(@"++++++++++++++++++");
        NSLog(@"++++++++++++++++++");
        NSLog(@"++++++++++++++++++");
        NSLog(@"++++++++++++++++++");

        // 
        UIAlertView *continueGame = [[UIAlertView alloc] initWithTitle:@"Fortsätta spel"
                                                              message:@"Det finns ett aktivt spel, klicka Spela eller Tillbaka"
                                                             delegate:self 
                                                    cancelButtonTitle:@"Tillbaka" 
                                                    otherButtonTitles:nil];

        [continueGame show];
        [continueGame release];


    }

    exit(0);
}


You are assigning onGoingGame to YES, not comparing it to YES. Use == instead of =.


Your alert code is just fine I use that form (three lines - init, show, release) all of the time to do alerts.

I suggest that the exit(0) is the root of the problem. If you want to exit after the user closes the alert, you should assign a delegate which will close the app when the user taps on the close button. Use your code, but remove the exit(0). Then implement the UIAlertViewDelegate as follows:

-(IBAction)continueGame_button:(id)sender {
    //=====CHECK IF THERE IS AN ON-GOING GAME, IF SO CONTINUE=====//
    AccessCurrentGameData *isThereAnOngoingGameFunction = [AccessCurrentGameData new];
    BOOL ongoingGame = [isThereAnOngoingGameFunction checkIfGameOngoing];
    [isThereAnOngoingGameFunction release];
    NSLog(@"+ + +continueGame_button+ + +");
    NSLog(@"ongoingGame = %@\n", (ongoingGame ? @"YES" : @"NO"));

    if (ongoingGame == YES) {
        NSLog(@"+++++++++ ONGOING GAME +++++++++");

        // 
        UIAlertView *continueGame = [[UIAlertView alloc] initWithTitle:@"Fortsätta spel"
                                                              message:@"Det finns ett aktivt spel, klicka Spela eller Tillbaka"
                                                             delegate:self 
                                                    cancelButtonTitle:@"Tillbaka" 
                                                    otherButtonTitles:nil];

        [continueGame show];
        [continueGame release];
    }
}

- (void) alertViewCancel:(UIAlertView *)alertView{
  //If you have other alerts, you may want to check the title of the alert to
  //make sure that you only exit when THIS alert is dismissed  
  exit(0);
}

Dont' forget to add the <UIAlertViewDelegate> code to your header (.h) file.

You can also use - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex, if you want multiple buttons, with one of them being a specific "Quit" button.

Please note that Apple discourages using exit() in apps that are released to the App Store, and using it might get your app rejected.


You can try this line instead.

[[[[UIAlertView alloc] initWithTitle:@"this is my message" message:nil delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil] autorelease] show];     

Also, I believe Apple does not advice using exit() within your application. They always want the user to use the "Home" button to exit an app. The exit() call is a hard exit and this might be the reason you are not seeing the Alert.


You should not release it immediately. And you exit the app even before the alert view gets a chance to display itself. :)

Your code will continue to run even when the alert view is visible.


Fixage

  1. Remove the exit call
  2. Don't release the alertview. Release it in it's owner's dealloc method.
  3. Make the alert view an instance variable and add a retain property to it.
  4. Initialize the alertview in its getter if it's not yet available.
  5. Set it's attributes in the IBAction and show it.
  6. Add the appropriate delegate methods.

If I wasn't writing this answer on an iPod touch I'd post some example code. You can find lots of such code with Google.


Also, if your app isn't English-only you should always use localization provided by Foundation. Otherwise you can get English text with default error messages and other UI elements.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜