我正在通过以下代码
创建一个警报视图控制器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
那我怎么能避免在这里强烈引用?
答案 0 :(得分:1)
尝试重写这样的闭包..
@lazy var alertHandler:(UIAlertAction!)->Void = { [unowned self] (UIAlertAction: action) -> () in
}