将IBAction添加到UIAlert

时间:2011-06-20 17:12:59

标签: iphone xcode ios4 xcode4

当我点击按钮时,我试图让我的UIAlert做两个不同的动作。当用户点击重新启动时,游戏重新开始,当点击主菜单时,游戏应该进入主菜单。重置按钮工作正常,但IBAction一直给我关于切换视图的错误。

// 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];

        }

    }



    }

重置工作正常,但我在IBAction上遇到两个错误。 'showFlip'未声明(首次使用此功能)和预期';'在':'标记之前。不明白为什么会这样说,因为当我在警报视图之外发布IBAction时,它运行正常。 任何帮助将不胜感激,提前谢谢

2 个答案:

答案 0 :(得分:4)

您正在定义一个方法,而不是调用一个方法!这段代码

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

        }

不应该住在这个功能里面。拉出来是

- (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];
}

答案 1 :(得分:4)

你应该试试这个:

// 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];
}