【问题标题】:Create an UIAlertAction in Swift在 Swift 中创建 UIAlertAction
【发布时间】:2015-01-25 14:04:40
【问题描述】:

我想创建以下UIAlertAction

@IBAction func buttonUpgrade(sender: AnyObject) {

           let alertController = UIAlertController(title: "Title",
        message: "Message",
        preferredStyle: UIAlertControllerStyle.Alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
        // ...
    }

    alertController.addAction(cancelAction)
}

我知道UIAlertController 被初始化为titlemessage,以及它更喜欢显示为警报还是操作表。

按下按钮时,我想显示警报,但alert.show() 不起作用。为什么我的代码不起作用?

【问题讨论】:

    标签: ios swift uialertcontroller


    【解决方案1】:

    这里的主要问题是UIAlertController(与UIAlertView 不同)是UIViewControlller 的子类,这意味着它需要这样呈现(而不是通过show() 方法)。除此之外,如果要将取消按钮的颜色更改为红色,则必须将取消操作的警报样式设置为.Destructive

    这仅在您希望按钮为红色时才有效。如果要将警报控制器中按钮的颜色更改为任意颜色,只能通过在警报控制器的view 属性上设置tintColor 属性来完成,这将更改其所有按钮的色调颜色(破坏性的除外)。需要注意的是,在 Apple 采用的设计范例中,由于取消按钮带有粗体文本,因此没有必要更改其颜色。

    如果您仍然希望文本是红色的,可以这样做:

    let alertController = UIAlertController(
        title: "Title",
        message: "Message",
        preferredStyle: UIAlertControllerStyle.Alert
    )
    
    let cancelAction = UIAlertAction(
        title: "Cancel",
        style: UIAlertActionStyle.Destructive) { (action) in
        // ...
    }
    
    let confirmAction = UIAlertAction(
        title: "OK", style: UIAlertActionStyle.Default) { (action) in
        // ...
    }
    
    alertController.addAction(confirmAction)
    alertController.addAction(cancelAction)
    
    presentViewController(alertController, animated: true, completion: nil)
    

    这会产生你所追求的结果:

    【讨论】:

    • 我仍然无法理解闭包的快速语法...in 应该是什么意思?为什么所有 inside 都是花括号?这需要时间来适应它......
    • in 只是将参数与闭包实现分开
    • 我听说的一个好方法可以帮助我记住 in 语法是如何使用的,在上面的例子中,“Action is starring IN the following function”。
    【解决方案2】:
    let alertController = UIAlertController(
        title: "Title",
        message: "Message",
        preferredStyle: UIAlertControllerStyle.Alert
    )
    
    let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
        // ...
    }
    
    let okayAction = UIAlertAction(title: "OK", style: .Default) { (action) in
        // ...
    }
    
    alertController.addAction(okayAction)    
    alertController.addAction(cancelAction)
    
    self.presentViewController(alertController, animated: true) {
        // ...
    }
    

    【讨论】:

    • 你把.Cancel放在哪里?
    • var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { UIAlertAction in NSLog("Cancel Pressed") } alert.addAction(cancelAction)
    • 请将其添加到您的答案中。
    • 谢谢。如何将“取消”的颜色更改为红色?谢谢!
    【解决方案3】:
    var alertController = UIAlertController(title: "Alert", message:"Message", preferredStyle: UIAlertControllerStyle.Alert)
    let confirmed = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
    let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)
    
    alertController.addAction(confirmed)
    alertController.addAction(cancel)
    self.presentViewController(alertController, animated: true, completion: nil)
    

    重要的是最后一行“self.presentViewController”实际显示您的警报。

    希望它有效;)

    【讨论】:

    • 感谢您的回答!我想要一个.Cancel 按钮。
    • 我正在搜索文档,但找不到任何有关处理程序参数的可空性的信息。但似乎传入 nil 是安全的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    相关资源
    最近更新 更多