我可以选择基本上在我的应用程序中打印文档。现在不允许打印一些文档(除非指定标准)。所以我正在使用代表。
请注意,我同时使用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
.
问题是我如何从这个警报视图返回一个布尔值。
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)
}