【发布时间】:2015-05-29 05:48:45
【问题描述】:
我刚刚创建了一个带有 ViewController 类的单视图应用程序项目。我想从位于我自己的类中的函数中显示 UIAlertController。
这是我的课程,带有警报。
class AlertController: UIViewController {
func showAlert() {
var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert)
self.presentViewController(alert, animated: true, completion: nil)
}
}
这是执行警报的 ViewController。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func showAlertButton(sender: AnyObject) {
var alert = AlertController()
alert.showAlert()
}
}
这是我得到的,而不是漂亮的警报。
警告:尝试在 Sprint1.AlertController: 0x797cc500 上呈现 UIAlertController: 0x797d2d20,其视图不在窗口层次结构中!
我该怎么办?
【问题讨论】:
-
有什么理由不使用 self.show()?
-
而不是 self.presentViewController(alert, animated: true, completion: nil) 使用 self.show()
-
“AlertController”没有名为“show”的成员。
-
如果你想让它表现得像一个普通的 uialertview,你可以让它继承 uialertview 而不是 uiviewcontroller。这就是你要找的吗?
-
UIAlertView 在 iOS 8 中已弃用。
标签: ios swift storyboard uialertview uialertcontroller