如何在Swift中访问UIAlertAction标题属性?
我有一个具有多个UIAlertActions的UIAlertController。我想访问所选操作的UIAlertAction title属性如何在Swift中访问UIAlertAction标题属性?,swift,uialertcontroller,Swift,Uialertcontroller,我有一个具有多个UIAlertActions的UIAlertController。我想访问所选操作的UIAlertAction title属性 let ac = UIAlertController(title: "Choose Filter", message: nil, preferredStyle: .actionSheet) ac.addAction(UIAlertAction(title: "CIBumpDistortion", s
let ac = UIAlertController(title: "Choose Filter", message: nil, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: "CIBumpDistortion", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIGaussianBlur", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIPixellate", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CISepiaTone", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CITwirlDistortion", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIUnsharpMask", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIVignette", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
访问ac.title属性只能访问AlertController title属性
查看苹果的文档,我可能需要使用
var title: String? { get }
但是,我不熟悉如何使用这种语法。文档中的语法向您展示了
title
属性是如何声明的,而不是如何在代码中引用它(尽管它可以提供一些见解)
由于它是一个属性,如果您有UIAlertAction
的实例,则可以通过点符号.title
访问它。幸运的是,您确实可以访问所选的UIAlertAction
:传递给处理程序的函数实例
在警报操作的处理程序中,即setFilter
,您可以访问其参数的.title
。这将是所选操作的标题
func setFilter(_ action: UIAlertAction) {
let selectedActionTitle = action.title
...
}