【问题标题】:Showing an alert view in Swift causes EXC BAD ACCESS [duplicate]在 Swift 中显示警报视图会导致 EXC BAD ACCESS [重复]
【发布时间】:2014-06-04 08:39:25
【问题描述】:

我有以下视图控制器。它只是从文本字段中读取一个值以显示在 UIAlertView 中。

import UIKit

class ViewController: UIViewController {

    @IBOutlet var textField: UITextField!

    @IBAction func pressButton(sender: UIButton) {
        let name = textField.text

        let alert = UIAlertView(
            title: "Hello!",
            message: "How are you today, \(name). I'm lovely!",
            delegate: nil,
            cancelButtonTitle: "Thanks!"
        )

        alert.show() // EXC_BAD_ACCESS
    }
}

为什么alert.show()EXC_BAD_ACCESS 发生崩溃?我的UIAlertView 实例在这里发生了什么?为什么不像我想的那样在alert 中?

【问题讨论】:

  • 我认为这是我没有遇到的语法问题。道歉!
  • iOS8 中UIAlertViews 不是被UIAlertController 替换了吗?

标签: ios swift


【解决方案1】:

尝试使用 var 而不是 lat 如下

var alert = UIAlertController(title: "Title", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

【讨论】:

  • 如何调用alert的委托方法。
  • @iOS Developer 这会导致背景视图变黑并生成警告,presenting view on deteched view controllers is discouraged
猜你喜欢
  • 2011-11-29
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多