我可以选择基本上在我的应用程序中打印文档。现在不允许打印一些文档(除非指定了条件)。所以我使用委托(delegate)。
请注意,我混合使用了 Objective C
和 Swift
。
基本上我的打印代码如下:
if ([self.delegate respondsToSelector:@selector(shouldPrintDocument)]) {
BOOL shouldPrint = [self.delegate shouldPrintDocument];
NSLog(@"Should Print %d", shouldPrint);
if (shouldPrint){
//We will print here
}
}
现在,在 Swift 方面,我基本上需要做的是与用户确认他们是否想要继续打印文档。因此,我使用 UIAlertController。
问题是如何从此警报 View 返回 bool 值。
func shouldPrintDocument() -> Bool {
let alertController = UIAlertController(title:"Confirm Print",
message: message,
preferredStyle: UIAlertControllerStyle.Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action: UIAlertAction) -> Void in
alertController.dismissViewControllerAnimated(true, completion: { _ in })
return false
})
alertController.addAction(cancelAction)
let ok: UIAlertAction = UIAlertAction(title: "Confirm", style: .Default, handler: {(action: UIAlertAction) -> Void in
alertController.dismissViewControllerAnimated(true, completion: { _ in })
//Perform some core data work here, i.e., save a few things and return
return true // This is where the issue comes in
})
alertController.addAction(ok)
self.presentViewController(alertController, animated: true, completion: nil)
}
最佳答案
您不会从警报 View 返回 bool 值。您的“ok”UIAlertAction 处理程序是您应该采取适当操作的地方。在那里检查文档是否应该打印,然后打印。或者从那里调用一个可以执行此操作的方法。但要从处理程序内部进行。处理程序是当前具有注释“//执行一些核心数据工作...”的代码块
关于ios - 显示 AlertView 后从 Function 返回 Bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36311225/