【问题标题】:UIAlertView with two buttons in iPhoneiPhone 中带有两个按钮的 UIAlertView
【发布时间】:2010-08-10 18:20:15
【问题描述】:

我正在尝试在按下按钮时显示警报视图,因此我编写了如下代码:

- (IBAction)signUpComplete: (id)sender {
  UIAlertView* alert_view = [[UIAlertView alloc]
      initWithTitle: @"test" message: @"test" delegate: nil cancelButtonTitle: @"cancel" otherButtonTitles: @"OK"];
  [alert_view show];
  [alert_view release];
}

但是这段代码在 initWithTitle 方法中由于以下异常而崩溃:

2010-08-11 03:03:18.697 Polaris[1155:207] *** -[UIButton copyWithZone:]:无法识别的选择器发送到实例 0x176af0
2010-08-11 03:03:18.700 Polaris[1155:207] *** 由于未捕获的异常而终止应用程序

0x176af0 与参数 'sender' 的值相同,即动作处理程序为 signUpComplete: 的按钮。我认为问题在于 otherButtonTitles: 参数,因为它与参数 nil 一起工作正常。因此,创建“确定”按钮存在问题。 我的代码有什么问题吗?

谢谢!

【问题讨论】:

  • 您能告诉我们您是如何创建按钮的吗?

标签: iphone cocoa-touch ios uialertview


【解决方案1】:

otherButtonTitles 列表必须是零终止的:

UIAlertView* alert_view = [[UIAlertView alloc]
      initWithTitle: @"test" message: @"test" delegate: nil 
      cancelButtonTitle: @"cancel" otherButtonTitles: @"OK", nil];

【讨论】:

  • @Vladimir 单击确定按钮时如何调用方法?
  • @Prashant,您可以使用 alertView:clickedButtonAtIndex: 委托方法,但 UIAlertView 在 iOS 9 中已被弃用,您应该改用 UIAlertController
  • @Vladimir,谢谢。想出了这个。我必须使用旧方法,因为它是当前的要求。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-26
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
相关资源
最近更新 更多