1

我可以选择基本上在我的应用程序中打印文档。现在不允许打印一些文档(除非指定标准)。所以我正在使用代表。

请注意,我同时使用Objective CSwift

基本上我的打印代码如下:

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

2 回答 2

1

您不会从警报视图返回布尔值。您的“ok” UIAlertAction 处理程序是您应该采取适当行动的地方。检查那里是否应该打印文档,然后打印。或者从那里调用一个方法来执行此操作。但是从处理程序内部进行。处理程序是您当前具有注释“//执行一些核心数据工作...”的代码块

于 2016-03-30T14:29:02.143 回答
0

尝试这个:

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)
}
于 2016-03-30T15:25:34.557 回答