2016-08-18 212 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