【问题标题】:Why doesn't this UIAlertController show?为什么这个 UIAlertController 不显示?
【发布时间】:2016-08-18 00:11:18
【问题描述】:

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

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)

【问题讨论】:

    标签: ios swift uialertview swift3 xcode8-beta4


    【解决方案1】:

    该代码与 Swift 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) 之后而不是在创建警报时关闭?

    希望这会有所帮助!

    【讨论】:

    • 非常感谢,等我回家试一试,可惜 swift3.0 还没有太多帮助。非常喜欢。
    猜你喜欢
    • 2023-03-14
    • 2013-07-12
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 2016-02-06
    相关资源
    最近更新 更多