【发布时间】:2016-07-23 13:01:33
【问题描述】:
我看到了更多这样的答案,但没有任何帮助。这是我较早的警报和操作
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 的首选样式
我试过了:
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 按钮没有任何动作,
请帮帮我,我怎样才能删除该警告并使用我的两个按钮操作重新编码我的 Uialert。
我是 swift 新手。您的帮助将很有用。谢谢!
【问题讨论】:
-
为什么在上述情况下需要按钮索引?在其处理程序块/闭包中编写每个按钮单击需要执行的代码。
-
@user5513630 如果我们使用 UIAlertCOntroller 而不是 UIAlertView ,那么我们可以使用这个函数“func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){} ”如果不是那么还有什么替代方法?请帮帮我,我是新人
-
@MidhunMP 如果我们使用 UIAlertCOntroller 而不是 UIAlertView ,那么我们可以使用这个函数“func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int){} ”如果不是那么还有什么替代方法?请帮帮我,我是新人
-
@ArgaPK:你不能使用那个方法。每个按钮都有一个闭包块(请参阅 UIAlertAction),当单击该按钮时,将执行相应的闭包。检查以下答案以获得清晰的想法
-
@MidhunMP 谢谢
标签: ios swift uialertview uialertcontroller