我一直在研究这个问题已经有一段时间了,找不到有效的解决方案。我正在创建一个游戏,为每个玩家分配一个随机名称。在游戏结束时,如果一个或多个玩家的分数符合他们的历史排行榜,我想循环遍历所有玩家,显示UIAlertView,他们可以输入他们的真实姓名,然后将该名称保存回来我的玩家对象。但是,我无法停止循环,显示警报视图,等待响应,然后继续。有人可以帮帮我吗?
继承我的循环:
for (int i = 0; i < [leaderboard count]; i++) {
NSDictionary *player = [[NSDictionary alloc] initWithDictionary:[leaderboard objectAtIndex:i]];
if ([[player valueForKey:@"isCurrentPlayer"] isEqualToString:@"YES"]){
// getRealPlayerName takes in a name parameter and displays an alertview
[self performSelectorOnMainThread:@selector(getRealPlayerName:) withObject:[player valueForKey:@"playerName"] waitUntilDone:YES];
// nameFromAlertView is a local variable that is set when the user enters a name in the alertview.
[player setValue:[self nameFromAlertView] forKey:@"playerName"];
[player setValue:@"" forKey:@"isCurrentPlayer"];
}
}
答案 0 :(得分:2)
你不能循环。
而是保存数组中的用户和点,调用显示UIAlertView的方法。从阵列中获取第一个用户信息,并从阵列中删除这些信息。设置警报视图的委托。一旦输入第一个信息,就会调用UIAlertView的委托方法(alertView:didDismissWithButtonIndex:
)。在那里你处理输入的信息,而不是检查,如果数组中还有什么东西,如果有的话,再次调用第一种方法
-(void)promptForInput
{
self.currentUserInfo = userArray[0]; //userarray is mutable, self.currentUserInfo is a property for the current user
[userArray removeObject:self.currentUserInfo];
//configure UIAlertView
alertView.delegate = self;
}
//once the user hit a altertview button, this delegate method gets called
-(void)alertView:(UIAlertView)alertView didDismissWithButtonIndex:(NSUInteger)idx
{
//save/process entered data, self.currentUserInfo hold the current user.
if([userArray count]> 0){
[self promptForInput];
}
}
如果您熟悉块,您还可以使用第三方代码,允许使用块而不是委托。但是在块中你会做的基本相同:调用另一个块,因为数组中的长用户是未处理的。
其中一项补充:Mugunth Kumar — Block Based UIAlertView and UIActionSheet
答案 1 :(得分:1)
仅显示警报视图,并在程序控制返回主runloop时处理事件。因此等待
[self performSelectorOnMainThread:@selector(getRealPlayerName:) withObject:[player valueForKey:@"playerName"] waitUntilDone:YES];
阻止警报视图显示。
你能做的是:
alertView:didDismissWithButtonIndex:
委托功能中,记录数据并为下一位玩家开始新的提醒视图(如果不是最后一位玩家)。您必须将当前玩家索引存储在班级的属性中。或者,您可以将当前索引分配给myAlertView.tag
。