首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertViewDelegate未按预期运行

UIAlertViewDelegate未按预期运行
EN

Stack Overflow用户
提问于 2012-10-20 23:05:37
回答 2查看 117关注 0票数 0

我有一个非常简单的过程运行,在每一轮简单的游戏后,计算分数,更新标签和所有正常的,非常简单的东西。我有一个UIAlertView,它可以通知玩家他/她的表现。我使用UIAlertViewDelegate来推迟所有的更新,重置控件等,直到UIAlertView被解除。方法有startNewRound、startOver和updateLabels。它们都做些什么是相当明显的。无论如何,当用户点击第十轮时,我创建了另一个UIAlertView,它通知玩家游戏已经结束,并显示总比分。再一次,我希望使用一个代表来推迟重置,直到AlertView被解散。唯一的问题是,对于endGame AlertView,它似乎使用了第一个AlertView的委托方法,导致游戏继续新一轮,而不是从头开始。我希望这是有意义的。不管怎样,下面是我的代码片段。

代码语言:javascript
复制
if (round == 10){
    UIAlertView *endGame = [[UIAlertView alloc]
                            initWithTitle: @"End of Game"
                            message: endMessage
                            delegate:self
                            cancelButtonTitle:@"New Game"
                            otherButtonTitles:nil];
    [endGame show];
}
else {
    UIAlertView *alertView = [[UIAlertView alloc]
                          initWithTitle: title
                          message: message
                          delegate:self
                          cancelButtonTitle:@"Next"
                          otherButtonTitles:nil];

    [alertView show];
}

然后是委托方法:

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self startNewRound];
    [self updateLabels];
}

- (void)endGame:(UIAlertView *)endGame didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self startOver];
}

所以就是这样。正如我所提到的,endGame AlertView似乎正在使用alertView的委托,因此没有激活[self startOver]方法。所有的方法都起作用了,只是AlertView使用了错误的委托方法。

致以敬意,

麦克

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-10-21 00:39:20

像这样修改你的代码

代码语言:javascript
复制
    if (round == 10){
        UIAlertView *endGame = [[UIAlertView alloc]
                                initWithTitle: @"End of Game"
                                message: endMessage
                                delegate:self
                                cancelButtonTitle:@"New Game"
                                otherButtonTitles:nil];
        endGame.tag = 111;
        [endGame show];
    }
    else {
        UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle: title
                              message: message
                              delegate:self
                              cancelButtonTitle:@"Next"
                              otherButtonTitles:nil];
        alertView.tag = 222;
        [alertView show];
    }

并将方法委托为,

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
    {
        if(alertView.tag == 111)
        {
            [self startNewRound];
            [self updateLabels];
        }
        else if(alertView.tag == 222)
        {
            [self startOver];

        }
     }
票数 2
EN

Stack Overflow用户

发布于 2012-10-20 23:17:49

你不能有两个委托方法来处理dismisswithbuttonindex,你需要用tag来处理这种情况。

给两个警告视图一个不同的标签,并在委托对象上检查它。因此,您可以区分这两个警报视图。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12989719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档