2015-11-26 27 views
-2
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    // Initialize Alert View 
    let alertController = UIAlertController(title: "No Internet connection", message: "Please Connect to Internet", preferredStyle: .Alert) 

    let EXITAction = UIAlertAction(title: "EXIT", style: .Destructive) {(action) in 
     //... 
     exit(0) 

    } 
    alertController.addAction(EXITAction) 

    self.presentViewController(alertController, animated: true) { 
     // ... 
    } 


} 

我試圖使用onLoad如果使用單擊然後退出。 但沒有發生。請幫助UIAlertController不能正常工作

(我很新的編程)

回答

6

你必須將你的代碼呈現UIAlertControllerviewDidAppear方法。

如果您將代碼修改爲viewDidLoad中的用戶界面,它將不會應用於視圖,因爲視圖控制器仍在加載。

+0

非常感謝。 –

0

只需聲明一個如下所示的函數,您可以在viewDidAppear()方法中將其稱爲alert("Message String")

func alert(info:String) { 
     let popUp = UIAlertController(title: "Title", message: info, preferredStyle: UIAlertControllerStyle.Alert) 
     popUp.addAction(UIAlertAction(title: "OK!", style: UIAlertActionStyle.Default, handler: {alertAction in 
      popUp.dismissViewControllerAnimated(true, completion: nil) 
     })) 
     self.presentViewController(popUp, animated: true, completion: nil) 
    }