我的单元格内有一个委托(delegate)方法,当我单击单元格内的批准按钮时调用该方法。
现在我希望此委托(delegate)方法具有返回类型 Bool,我将使用它来更新单元格中的 UI。
optional func didTapApproveButton(indexpath: NSIndexPath ) -> Bool
这是委托(delegate)方法。 我想将返回类型保留为可选,因为我将在 VC 的方法内进行 Web 服务调用,并根据响应返回 true 或 false。
如何将返回的 BOOL 保持为可选
编辑
这就是我在VC中使用方法的方式
func didTapApproveButton(indexpath: NSIndexPath) -> Bool? {
let user = self.dataCtlr.arrayOfUsers[indexpath.row];
if(Reachability.isConnectedToNetwork()){
self.dataCtlr.approveRejectUser(true, forUser: user, onSuccess: { (shouldRefreshView) in
return true
}, onFailure: { (result) in
return false
});
}
else{
UserDefaultsManager.sharedInstance.addApproveReject((user.id)!, isApproved: true);
return true
}
return nil
}
现在 return true 或 false 不会返回到单元格,以便我可以更新单元格。
PS:我已经将协议(protocol)更新为快速实现
protocol PendingRequestsTableViewCellDelegate{
func didTapApproveButton(indexpath: NSIndexPath ) -> Bool?
}
最佳答案
在你的情况下你不能使用 Bool?
因为你的协议(protocol)是@objc。所以你将无法使用像可选值这样的快速特定能力。
我会建议您删除 @obj-c
和 Optional
方法
协议(protocol) CellDelegate {
func didTapApproveButton(indexpath: IndexPath ) -> Bool?
关于ios - 委托(delegate)中的可选返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44198438/