如何在UIAlertViewController的回调中避免强引用

时间:2014-08-21 12:36:57

标签: ios iphone swift ios8 xcode6

我正在通过以下代码

创建一个警报视图控制器
var alertView = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alertView.addAction(UIAlertAction(title: NVConstant.alertInfoBtnTitle, style: UIAlertActionStyle.Default, handler: alertHandler))

我的警报处理程序代码如下:

@lazy var alertHandler:(UIAlertAction!)->Void = { a![enter image description here][1]ction in
    var clickedButtonTitle:String = action.title
    switch clickedButtonTitle{
    case NVConstant.notificationAlertConfirmatinTitle :
        Utility.cancelAlarmForTheActivity(activity: self.selectedActivity.0)
    case NVConstant.notificationAlertCancelTitle :
        self.selectedSwitch.setOn(true, animated: true)
    default :
        return
    }

}

所以我的问题是如何在回调闭包中避免强引用self。我尝试使用[unowned self],但在应用程序崩溃后,我认为由于参数不匹配。

@lazy var alertHandler:(UIAlertAction!)->Void = { [unowned self] action in  //Crash 
// code
}

以下是崩溃: http://i.stack.imgur.com/JfgNi.png

那我怎么能避免在这里强烈引用?

1 个答案:

答案 0 :(得分:1)

尝试重写这样的闭包..

@lazy var alertHandler:(UIAlertAction!)->Void = { [unowned self] (UIAlertAction: action) -> () in

}