我有一个非常简单的过程运行,在每一轮简单的游戏后,计算分数,更新标签和所有正常的,非常简单的东西。我有一个UIAlertView,它可以通知玩家他/她的表现。我使用UIAlertViewDelegate来推迟所有的更新,重置控件等,直到UIAlertView被解除。方法有startNewRound、startOver和updateLabels。它们都做些什么是相当明显的。无论如何,当用户点击第十轮时,我创建了另一个UIAlertView,它通知玩家游戏已经结束,并显示总比分。再一次,我希望使用一个代表来推迟重置,直到AlertView被解散。唯一的问题是,对于endGame AlertView,它似乎使用了第一个AlertView的委托方法,导致游戏继续新一轮,而不是从头开始。我希望这是有意义的。不管怎样,下面是我的代码片段。
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];
}然后是委托方法:
- (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使用了错误的委托方法。
致以敬意,
麦克
发布于 2012-10-21 00:39:20
像这样修改你的代码
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];
}并将方法委托为,
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 111)
{
[self startNewRound];
[self updateLabels];
}
else if(alertView.tag == 222)
{
[self startOver];
}
}发布于 2012-10-20 23:17:49
你不能有两个委托方法来处理dismisswithbuttonindex,你需要用tag来处理这种情况。
给两个警告视图一个不同的标签,并在委托对象上检查它。因此,您可以区分这两个警报视图。
https://stackoverflow.com/questions/12989719
复制相似问题