如果用戶在沒有互聯網連接的情況下打開應用程序,會彈出一個窗口,提示需要連接,並且有一個ok按鈕。我想要ok按鈕退出應用程序。以下是我有:使用UIAlertController退出應用程序
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按鈕的上方。
這是'handler'參數是做什麼用的'UIAlertAction'確定。 – rmaddy
您是否使用Apple的Reachability?如果是這樣,您可以在屏幕上顯示一條消息並禁用用戶交互。當互聯網連接回來時,您可以啓用用戶交互並擺脫消息。在這種情況下,我會使用標籤而不是警報。 –