我正在尝试在应用程序首次加载时添加UIAlertView或Controller.目前,我的方法中有这个代码viewDidLoad.
let welcomeAlert = UIAlertController(title: "hola", message: "this is a test.", preferredStyle: UIAlertControllerStyle.Alert)
welcomeAlert.addAction(UIAlertAction(title: "ok.", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(welcomeAlert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
为什么不显示警报视图?我使用相同的代码,但在IBAction一个按钮内,它按预期工作.
您应该在viewDidAppear:功能中显示警报.要显示子视图或其他视图控制器,父视图控制器必须位于视图层次结构中.
该
viewDidAppear功能"通知视图控制器其视图已添加到视图层次结构中".*来自文档
所以,你的代码看起来像这样:
class MyViewController: UIViewController {
override func viewDidAppear(animated: Bool){
super.viewDidAppear(animated)
let welcomeAlert = UIAlertController(title: "hola", message: "this is a test.", preferredStyle: UIAlertControllerStyle.Alert)
welcomeAlert.addAction(UIAlertAction(title: "ok.", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(welcomeAlert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
451 次 |
| 最近记录: |