use*_*630 33 uialertview ios swift uialertcontroller
我看到了更多的答案,但没有任何帮助.这是我的旧警报和行动
override func viewWillAppear(animated: Bool) {
if Reachability.isConnectedToNetwork() == true {
print("internet connection ok")
} else
{
print("internet not ok")
let alertView: UIAlertView = UIAlertView(title: "Alert ", message: "connect to internet", delegate: self, cancelButtonTitle: "settings", otherButtonTitles: "cancel")
alertView.show()
return
}
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
{
if buttonIndex == 0 {
//This will open ios devices wifi settings
UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!)
}
else if buttonIndex == 1
{
//TODO for cancel
exit(0)
}
}
Run Code Online (Sandbox Code Playgroud)
在那我得到警告:
'UIAlertView'在iOS 9.0中已弃用.使用UIAlertController而不是UIAlertControllerStyleAlert的preferredStyle
我试过了 :
let alert = UIAlertController(title: "Alert", message: "My Alert for test", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: { (action:UIAlertAction!) in
print("you have pressed the Cancel button")
}))
self.presentViewController(alert, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
但是要添加两个按钮并添加按钮的索引路径按下方法链接我的旧代码,我无法做到这一点.我的uialert按钮没有任何动作,
请帮帮我,我怎么能删除那些警告并用我的两个按钮动作重新编码我的Uialert.
我是swift的新手.你的帮助会很有用.谢谢!
Kir*_*odi 71
请参阅以下代码中的Code Destructive和OK按钮UIAlertController:
let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert
let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.Destructive) {
(result : UIAlertAction) -> Void in
print("Destructive")
}
// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
}
alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.presentViewController(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
let alertController = UIAlertController(title: "Destructive", message: "Simple alertView demo with Destructive and Ok.", preferredStyle: UIAlertControllerStyle.alert) //Replace UIAlertControllerStyle.Alert by UIAlertControllerStyle.alert
let DestructiveAction = UIAlertAction(title: "Destructive", style: UIAlertActionStyle.destructive) {
(result : UIAlertAction) -> Void in
print("Destructive")
}
// Replace UIAlertActionStyle.Default by UIAlertActionStyle.default
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
(result : UIAlertAction) -> Void in
print("OK")
}
alertController.addAction(DestructiveAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
请参阅具有破坏性和确定按钮的警报:
Fre*_*usa 15
在Swift 3中,您可以这样写:
let alertController = UIAlertController(title: "Title", message: "This is my text", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default)
{
(result : UIAlertAction) -> Void in
print("You pressed OK")
}
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
9Ba*_*nap 10
对于基本警报消息,我喜欢在UIViewController上使用扩展:
extension UIViewController {
func alertMessageOk(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
self.alertMessageOk(title: "Test Title", message: "Test message")
| 归档时间: |
|
| 查看次数: |
46547 次 |
| 最近记录: |