Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift中访问UIAlertAction标题属性?_Swift_Uialertcontroller - Fatal编程技术网

如何在Swift中访问UIAlertAction标题属性?

如何在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

我有一个具有多个UIAlertActions的UIAlertController。我想访问所选操作的UIAlertAction title属性

    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
    ...
}