我还没有收到任何错误,但我只是想知道它是否是合法的"要做到这一点,让我们考虑以下示例:
class SomeView : UIViewController {
var alertView:UIAlertView = UIAlertView()
@IBAction func buttonOne(sender: UIButton) {
alertView.title = "Button Pressed 1"
alertView.message = "Button Message 1"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
@IBAction func buttonOne(sender: UIButton) {
alertView.title = "Button Pressed 2"
alertView.message = "Button Message 2"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
}
}
这是使用UIAlertView
的有效方式,还是每次使用时都需要声明它?只是为了确保将来不会成为问题。
答案 0 :(得分:4)
在iOS 8中,不推荐使用UIAlertView。现在UIAlertController是一个单独的类,用于创建和交互我们所知的UIAlertView。这是创建它的方法: -
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
创建处理警报事件的处理程序
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
switch action.style{
case .Default:
println("default")
break
case .Cancel:
println("cancel")
break
case .Destructive:
println("destructive")
break
}
}))
self.presentViewController(alert, animated: true, completion: nil)
Courtsey: - http://ashishkakkad.wordpress.com/2014/07/21/working-with-alert-in-swift-language-ios-8-xcode-6/