I have an option to print a document basically in my app. Now a few documents aren't allowed to be printed (unless a criteria is specified). So I'm using delegates.
Note that I'm using a mix of both Objective C
and Swift
.
Basically my print code is as follows:
if ([self.delegate respondsToSelector:@selector(shouldPrintDocument)]) {
BOOL shouldPrint = [self.delegate shouldPrintDocument];
NSLog(@"Should Print %d", shouldPrint);
if (shouldPrint){
//We will print here
}
}
Now at Swift
side of things, what I essentially need to do is confirm with the user if they want to proceed with printing of the document. So, I use a UIAlertController
.
The question is how do I return a bool value from this alert view.
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)
}