为什么这个UIAlertController不显示?

时间:2016-08-18 00:11:19

标签: ios swift uialertview swift3 xcode8-beta4

在尝试实施UI警报时,我遇到了一些问题。我在Xcode 8 beta 4中使用swift 3.0,我试图有一个激活警报的按钮,一个按钮(取消)解除警报,另一个(ok)执行一个动作就像UIAction Button那样,但我一直无法甚至得到警告以显示。

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)

1 个答案:

答案 0 :(得分:3)

该代码与Swift 3不兼容。var alertResult = alert('Btn 1 clicked'); button.addEventListener('click', alertResult); // say waaat? var alertFunction = function() { alert('Btn 1 clicked'); }; button.addEventListener('click', alertFunction); // Yess! 之类的内容现在是.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) 之后而不是在创建警报时有关闭?

希望这有帮助!