Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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
Ios 为什么可以';我不能在函数外声明一个处理程序吗?_Ios_Swift_Block_Handler - Fatal编程技术网

Ios 为什么可以';我不能在函数外声明一个处理程序吗?

Ios 为什么可以';我不能在函数外声明一个处理程序吗?,ios,swift,block,handler,Ios,Swift,Block,Handler,我可以在函数中声明这样的处理程序: @IBAction func handleActionSheetPressed(sender: UIButton) { let dismisHandler:(UIAlertAction)->Void = {(action:UIAlertAction) in self.dismissViewControllerAnimated(true , completion: nil) } 但是,当我在函数外部

我可以在函数中声明这样的处理程序:

    @IBAction func handleActionSheetPressed(sender: UIButton) {
        let dismisHandler:(UIAlertAction)->Void = {(action:UIAlertAction) in
            self.dismissViewControllerAnimated(true , completion: nil)
    }
但是,当我在函数外部声明此处理程序时,出现了一个错误:

“类型为'NSObject->()->ViewController'的值没有成员。” “dismissViewControllerAnimated”


有人能告诉我如何在函数外部声明此处理程序吗?

您不能在类属性中的闭包声明中使用
self
实例,因为此变量可以在另一个类上使用。 因此,必须为要调用的视图控制器添加一个额外参数

var dismisHandler:(UIAlertAction, UIViewController)->Void = {(action:UIAlertAction, vc) in

        vc.dismissViewControllerAnimated(true, completion: nil)
    }

在哪个类中定义了
handleActionSheetPressed
?请完整地向我们展示您的代码。UIViewController的子类