ios - 显示 AlertView 后从 Function 返回 Bool 值

标签 ios objective-c swift uialertview uialertaction

我可以选择基本上在我的应用程序中打印文档。现在不允许打印一些文档(除非指定了条件)。所以我使用委托(delegate)。

请注意,我混合使用了 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。

问题是如何从此警报 View 返回 bool 值。

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

最佳答案

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

关于ios - 显示 AlertView 后从 Function 返回 Bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36311225/

相关文章:

ios - 使用 Google 跟踪代码管理器在 iOS 上获取唯一设备 ID

ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音

ios - 在 iOS 6 中无法使用相机扫描 PDF 417 条码

objective-c - NSMutableArray 使用选择器排序

ios - 我如何获得 uibezierPath 的面积和周长,因为 fill() 方法使用了封闭区域

ios - Swift Sprite Kit 相机运动

ios - iPhone GPS 用户位置在手机静止时来回移动

ios - 自定义 UIWindow 及其根 UINavigationController 未被删除

objective-c - NSFileHandle -readDataOfLength : return autoreleased NSData? 是否

ios - 在 Xcode 10 上出现错误 : "Swift does not support the SDK ' iPhoneSimulator11. 2.sdk'"