2016-11-05 75 views

回答

1

在新控制器的viewDidLoad()方法,创建一个新的UIAlertController和类似下面的

let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert) 

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in 
    // ... 
} 
alertController.addAction(cancelAction) 

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in 
    // ... 
} 
alertController.addAction(OKAction) 

self.presentViewController(alertController, animated: true) { 
    // ... 
} 

注意,这个例子来自NSHipster网站,该网站提供了有关iOS版的好文章所显示。你可以找到关于UIAlertController here的文章。他们还解释了你可以用这个类做的其他事情,例如显示一个Action Sheet。

0

斯威夫特4
与你的函数创建的UIViewController的扩展,以显示与所需的参数参数警报

extension UIViewController { 

     func displayalert(title:String, message:String) { 
     let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction((UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in 

      alert.dismiss(animated: true, completion: nil) 

     }))) 

     self.present(alert, animated: true, completion: nil) 


     } 
} 


现在从您的视图控制器调用这个函数:

class TestViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.displayalert(title: <String>, message: <String>) 
    } 
} 
相关问题