【问题标题】:UIAlertView without any buttons没有任何按钮的 UIAlertView
【发布时间】:2011-09-20 14:04:18
【问题描述】:

我想知道下面的代码是否可以。我试图通过“timedAlert”方法在 2 秒后自动关闭 alertView(并且在 alertView 中没有任何按钮)。

    //this is in another method  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
    [alert release];
    [self timedAlert];
}

-(void)timedAlert
{
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:2];
}

-(void)dismissAlert:(UIAlertView *) alertView
{
    [alertView dismissWithClickedButtonIndex:nil animated:YES];
}

如果alertView的cancelButton设置为“nil”,“[alertViewdismissWithClickedButtonIndex:0 animated:YES];”怎么办?东西工作???我尝试将cancelButton设置为“nil”并且它有效,但无法弄清楚如何......

P.S:我从另一个调用 timedAlert 方法

感谢任何帮助!谢谢!

【问题讨论】:

  • 是的,你的代码没问题。你可以在这里阅读更详细的解释:iphonedevelopertips.com/user-interface/…
  • 谢谢!但是当alertView中没有按钮时,我仍然知道dismissWithClickedBittonIndex如何正常工作。尤其是为什么接受按钮索引...请帮助!
  • 我不太明白你的问题是什么。在dismissWithClickedBittonIndex 方法中,按钮索引是可选的。如果您传递 nil(或 0),则警报会简单地关闭。
  • 好的。谢谢你的帮助!这就是我想知道的...... :)

标签: objective-c iphone uialertview


【解决方案1】:

首先让我说,如果您使用自定义视图处理这个问题会更好,但话虽如此,问题似乎出在

[alert release];

你在完成之前释放对象(我很惊讶它没有崩溃)。

做这样的事情

// other code
alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:3.0f];
}

-(void)dismissAlert:(UIAlertView *) alertView
{
    [alertView dismissWithClickedButtonIndex:nil animated:YES];
    [alertView release];
}

【讨论】:

  • 它不会崩溃,因为 [alert show] 保留了对象并且“alert”变量仍然只是指向它。此答案中修改后的代码是可以的,但是可以将 [alert release] 留在 [self performSelector....] 之后的代码中,并从dismissAlert 视图中删除。或者现在就搬到 ARC 来摆脱所有这些混乱。
【解决方案2】:

您的代码应该可以工作,并且应该没有问题。我已经在我以前的一个应用程序中做到了这一点。该按钮未显示,因为标题为零,但我认为该按钮的实例仍然存在。在关闭警报之前放置一个断点并查看警报变量,并检查是否有按钮数组或其他东西,它应该告诉你它是如何工作的。

【讨论】:

  • 还有 NSInteger 值读取 -1 用于取消按钮、默认按钮和第一个其他按钮.. 对吗?那是因为dismissWithClickedButtonIndex接受索引的nil值??????
  • 我还看到其中的“_dismissButtonIndex”字段读取 0 值!我希望这说明了一切!我说的对吗?
猜你喜欢
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多