开发者

Adding IBAction to a UIAlert

I was trying to get my UIAlert to do two different actions when the buttons are clicked. 开发者_如何学CWhen the user clicks restart, the game restarts and when main menu is clicked, the game should go to the main menu. The reset button is working fine but the IBAction keeps giving me errors about switching views.

// called when the player touches the "Reset Game" button
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0)
    {
        [self resetGame];
    }
    else
    {
        - (IBAction)showFlip:(id)sender {
            Menu *menuView = [[[menu alloc] init] autorelease];
            [gameView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            [self presentModalViewController:menuView animated:YES];

        }

    }



    }

The reset works fine but I am getting two errors on the IBAction. 'showFlip' undeclared (first use in this function) and expected ';' before ':' token. Don't understand why it would say this because when I post the IBAction outside of the alertview it works fine. Any help would be appreciated, thanks in advance


You are defining a method, not calling one! This code

- (IBAction)showFlip:(id)sender {
            Menu *menuView = [[[menu alloc] init] autorelease];
            [gameView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            [self presentModalViewController:menuView animated:YES];

        }

should not live inside this function. Pull it out to be

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0)
    {
        [self resetGame];
    }
    else
    {
        [self showFlip];
    }
}

-(void)showFlip{
    Menu *menuView = [[[menu alloc] init] autorelease];
    [gameView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:menuView animated:YES];
}


You should try this:

// called when the player touches the "Reset Game" button
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0)
    {
        [self resetGame];
    }
    else
    {
        [self showFlip:nil];
    }
}

- (IBAction)showFlip:(id)sender {
    Menu *menuView = [[[menu alloc] init] autorelease];
    [gameView setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [self presentModalViewController:menuView animated:YES];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜