ios - 如何在 UIAlertAction 中显示选定的操作?

标签 ios swift uialertcontroller

我试图通过一些绿色操作来实现 View ,并让所选操作在 UIAlertController 中以粗体显示。其中一个操作(即关闭按钮)必须与其他操作分开,并用红色着色。

我正在尝试使用 .cancel 允许显示关闭按钮的样式添加它们,但它是粗体,但是是绿色的。 我该如何实现这一目标?

预期 View :

expected view

我当前的代码:

let alertController
        = UIAlertController(title: nil,
                            message: nil,
                            preferredStyle: .actionSheet)

    let sortByDateAction = UIAlertAction(title: "По дате",
                                         style: .default,
                                         handler: {(action: UIAlertAction!) in
                                            if self.sortBy != "date" {
                                                self.page = 1
                                                self.sortBy = "date"
                                                self.loadTenders()
                                            }
    })
    let moreExpensiveSortAction = UIAlertAction(title: "Дороже",
                                                style: .destructive,
                                                handler: {(action: UIAlertAction!) in
                                                    if self.sortBy != "priceHigh" {
                                                        self.page = 1
                                                        self.sortBy = "priceHigh"
                                                        self.loadTenders()
                                                    }
    })
    let cheaperSortAction = UIAlertAction(title: "Дешевле",
                                          style: .default,
                                          handler: {(action: UIAlertAction!) in
                                            if self.sortBy != "priceLow" {
                                                self.page = 1
                                                self.sortBy = "priceLow"
                                                self.loadTenders()
                                            }
    })

    alertController
        .addAction(sortByDateAction)
    alertController
        .addAction(moreExpensiveSortAction)
    alertController
        .addAction(cheaperSortAction)

    alertController.preferredAction = sortByDateAction

    alertController
        .addAction(UIAlertAction(title: "Dismiss",
                                 style: .cancel,
                                 handler: nil))

最佳答案

let cancelAlert = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler:nil)
        cancelAlert.setValue(UIColor.red, forKey: "titleTextColor")
        alertController.addAction(cancelAlert)

试试这个代码。

关于ios - 如何在 UIAlertAction 中显示选定的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44404899/

相关文章:

ios - 在调用 awakeFromNib 之前尝试在 UIView 中设置属性

ios - 点击以在 Swift 中重试连接互联网

ios - 如何用其他符号替换日期组件分隔符?

ios - 向 MKMapView 注释添加操作

ios - [NSCFString timeIntervalSinceReferenceDate] : unrecognized selector sent to instance

swift - 如何在协议(protocol)中定义仅在协议(protocol)的关联类型为特定类型时才需要实现的函数

具有复杂类型的 Swift CustomStringConvertible

ios - Swift 中的 UIAlert 会自动消失吗?

ios - 带有延迟标题和消息的 actionSheet 样式警报

ios - 在 Swift 中制作 Gif 动画