使用UIAlertController

时间:2016-04-04 15:03:55

标签: ios swift uialertcontroller uicontrol

如果用户在没有互联网连接的情况下打开应用程序,则会弹出一个窗口,显示需要连接,并且有一个确定按钮。我想按ok按钮退出应用程序。这就是我所拥有的:

if !isConnectedToNetwork(){
    let alert = UIAlertController(title: "No Internet", message: "You need an internet connection to use this app", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

我将使用它来退出应用程序:

UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)

我只是不知道如何将它连接到上面的OK按钮。

2 个答案:

答案 0 :(得分:4)

别。 Apple会拒绝这一点(如果他们看到的话)。

只需告知用户并添加“重试”按钮即可。重试按钮显然应该再次检查连接。

要真正回答这个问题,您当前在按钮操作上设置了handler: nil,而实际上您可以设置一个处理程序并使用它来调用您喜欢的任何逻辑。

答案 1 :(得分:2)

当用户通过以下代码按时,您可以处理

alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,
handler: { (action:UIAlertAction!) -> Void in
   //after user press ok, the following code will be execute
   NSLog("User pressed OK!!")
}))
相关问题