2016-08-18 210 views
2

在嘗試實現UI警報時,我遇到了一些問題。我正在使用Xcode 8 beta 4中的swift 3.0,我試圖讓一個按鈕激活一個警報,一個按鈕(取消)取消警報另一個(ok)執行一個動作作爲UIAction按鈕,但是我一直無法甚至得到一個警報顯示。爲什麼不顯示此UIAlertController?

var warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .Alert) 

var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { 
    UIAlertAction in 
    NSLog("OK Pressed") 
} 

var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { 
    UIAlertAction in 
    NSLog("Cancel Pressed") 
} 

warning.addAction(okAction) { 
    // this is where the actions to erase the content in the strings 
} 
warning.addAction(cancelAction) 

self.presentViewController(warning, animated: true, completion: nil) 

回答

3

該代碼不兼容斯威夫特3之類的東西.Alert現在.alert是。和presentViewController方法完全不同。

這應該工作。

let warning = UIAlertController(title: "warning", message: "This will erase all content", preferredStyle: .alert) 

    let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { 
     UIAlertAction in 
     NSLog("OK Pressed") 
     //ok action should go here 
    } 


    let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel) { 
     UIAlertAction in 
     NSLog("Cancel Pressed") 
    } 

    warning.addAction(okAction) 
    warning.addAction(cancelAction) 

    present(warning, animated: true, completion: nil) 

爲什麼您在addAction(okAction)之後關閉而不是在您創建警報時?

希望這會有所幫助!

+0

非常感謝你,當我回家的時候給它一個嘗試,但它對於swift3.0還沒有太多幫助。大大appriecated。 – Yellow