简体   繁体   中英

Return a Bool value from Function after showing AlertView

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)
}

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..."

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM