'UIAlertView'在iOS 9.0中已弃用。使用UIAlertController而不是UIAlertControllerStyleAlert的preferredStyle [英] 'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

查看:101
本文介绍了'UIAlertView'在iOS 9.0中已弃用。使用UIAlertController而不是UIAlertControllerStyleAlert的preferredStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到更多答案,但没有任何帮助。这是我的旧警报和行动

I have see more so answers , but nothing helped.Here is my older alert and action for that

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) 
    }
}

因为我收到警告:


'UIAlertView'在iOS 9.0中已弃用。使用UIAlertController和UIAlertControllerStyleAlert的
preferredStyle代替

'UIAlertView' was deprecated in iOS 9.0. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

我试过:

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)

但要添加两个按钮并添加按钮的索引路径按下方法链接我的旧代码,我无法做到这一点。我的uialert按钮没有任何动作,

But to add two button and add the index path of button press method link my older code,I am not able to do that. Nothing action happening fro my uialert button,

请帮帮我,如何删除警告并用我的两个按钮操作重新编码我的Uialert。

Please help me out,How can i remove that warnings and recode my Uialert with my two button action.

我是swift的新手。你的帮助会很有用。谢谢!

I am new to swift.Your help will be useful.Thanks!

推荐答案

看到这个 UIAlertController中的Code Destructive和OK按钮

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)

Swift 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)

查看具有破坏性和OK按钮的警报:

这篇关于'UIAlertView'在iOS 9.0中已弃用。使用UIAlertController而不是UIAlertControllerStyleAlert的preferredStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆