2

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)
}
Gaurav Wadhwani
  • 1,352
  • 2
  • 15
  • 32
  • You should almost certainly do that in a closure. Your function isn't going to wait for another closure to return without being blocking. It should return as soon as possible and pass a bool into its completion. – Dare Mar 30 '16 at 14:10
  • @Dare Hi, yes but I'm not sure how to go about that? Could you point me to the right direction? – Gaurav Wadhwani Mar 30 '16 at 14:40
  • You can use actionsheet delegate function from where you can get the return values – Balaji Kondalrayal Mar 30 '16 at 14:59
  • @BalajiKondalrayal ActionSheet is deprecated in iOS8. So I'd prefer not to use that – Gaurav Wadhwani Mar 30 '16 at 15:02
  • Ok.. Then use the global variable for the particular class and assign the return value in the action handler of the alert action button – Balaji Kondalrayal Mar 30 '16 at 15:08
  • @BalajiKondalrayal yes, but the function doesn't wait for the value to be assigned. It just moves on. One option is to loop continuously until a particular value is set but I don't think its an efficient solution – Gaurav Wadhwani Mar 30 '16 at 15:09

2 Answers2

2

You don't return a bool from an alert view. Your "ok" UIAlertAction handler is the place where you should take the appropriate action. Check there whether the document should be printed and then print it. Or call a method from there that will do this. But do it from within the handler. The handler is the block of code where you currently have the comment "// Perform some core data work..."

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • The document printing options are in a separate PDF viewer class - so I'm trying to avoid moving my Core Data code there. When the print button is tapped, that PDF class asks its delegate if its okay to go ahead and stuff. Which is why I'm using this approach. – Gaurav Wadhwani Mar 30 '16 at 14:41
  • This has nothing to do with Core Data. You are asking the user to confirm whether to print the document. The alert is sitting on the screen for who knows how long, waiting for the user to respond. When the user taps "Confirm", your handler is getting called. That's the right place to check if the document can be printed and to launch the printing. You don't need to move anything, just call your PDF viewer class from the handler. You can call Objective-C from Swift. – Mike Taverne Mar 30 '16 at 15:14
0

Try this:

var isprint:BOOL = false

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 })
    isprint = 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
    isprint = true// This is where the issue comes in
})

alertController.addAction(ok)
 self.presentViewController(alertController, animated: true, completion: nil)
}
Balaji Kondalrayal
  • 1,743
  • 3
  • 19
  • 38