【问题标题】:Alert that can work on iOS 7 and iOS 8可在 iOS 7 和 iOS 8 上运行的警报
【发布时间】:2014-07-26 08:22:57
【问题描述】:

我得到 dyld:找不到符号:_OBJC_CLASS_$_UIAlertAction 当我试图让这个怪物运行时。

我如何弱链接 8.0 的东西?

var device : UIDevice = UIDevice.currentDevice()!;
var systemVersion = device.systemVersion;
var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue;
if(iosVerion < 8.0) {
    let alert = UIAlertView()
    alert.title = "Noop"
    alert.message = "Nothing to verify"
    alert.addButtonWithTitle("Click")
    alert.show()
} else {
    var alert : UIAlertController? = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert)
    if alert {
        let actionStyle : UIAlertActionStyle? = UIAlertActionStyle.Default;
        var alertAction : UIAlertAction? = UIAlertAction(title: "Click", style: actionStyle!, handler: nil)
        if(alertAction) {
            alert!.addAction(alertAction)
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }
}
return;

已解决:UIKit 必须标记为可选而不是必需。现在是简化版:

var device : UIDevice = UIDevice.currentDevice()!;
var systemVersion = device.systemVersion;
var iosVerion : Float = systemVersion.bridgeToObjectiveC().floatValue;
if(iosVerion < 8.0) {
    let alert = UIAlertView()
    alert.title = "Noop"
    alert.message = "Nothing to verify"
    alert.addButtonWithTitle("Click")
    alert.show()
} else {
    var alert : UIAlertController = UIAlertController(title: "Noop", message: "Nothing to verify", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Click", style:.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

【问题讨论】:

  • 这是您提到的问题的超集。其他人建议 let alert = UIAlertView() alert.title = "Noop" alert.message = "Nothing to verify" alert.addButtonWithTitle("Click") alert.show() 这对我有用。肯定有人应该找到 Apple 关于 UIAlertView shim 上的便利性 inti 损坏的错误
  • 我做到了。引用的条目与 UIAlertView 的初始化有关。我的参赛作品与吃蛋糕和吃蛋糕有关。
  • “UIKit 必须标记为可选而不是必需”是什么意思?

标签: ios backwards-compatibility swift


【解决方案1】:
  • 在项目构建阶段的“Link Binary With Libraries”部分中显式添加 UIKit。

  • 你可以像这样测试 UIAlertController 的存在:

    if NSClassFromString("UIAlertController") != nil {
        // Use it
    } else {
        // Fall back
    }
    
  • 我编写了一个适用于 iOS 7 和 iOS 8 的包装器。您可以find it here。它需要一个视图控制器,后跟一堆可选参数和任意数量的按钮:

    showAlert(self, style: .ActionSheet, sourceView: cell, completion: {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    },
        (.Default, "Send clipboard", {
            if someCondition {
                // completion must be specified because of a Swift bug (rdar://18041904)
                showAlert(self, title: "Nothing to send", message: "The clipboard is empty.", completion: nil,
                    (.Cancel, "OK", nil)
                )
            }
        }),
        (.Cancel, "Cancel", nil)
    )
    

【讨论】:

  • 我还没有测试过这个,但这就是我打算做的,一个包装器。为这项努力竖起大拇指。无论如何,我的计划与你的不同之处在于我会用 swift 语言创建一个包装器,但我会在 object-c 中使用它。
  • 这看起来很酷。然而,虽然它在 iOS8 上完美运行,但在 iOS7 上它按预期显示警报视图,但它没有调用两个按钮的完成处理程序。我做错了什么? showAlert(tabBarController, style: .Alert, title: "需要登录", message: "必须先登录才能使用此功能。", sourceView: nil, completion: nil, (.Cancel, "Cancel", {println( "ko")}), (.Default, "Login", { println("ok") }))
  • 我尝试复制/粘贴您自己的示例,但即使在 iOS7 上对我也不起作用
【解决方案2】:

在 Xcode 6 beta 5 中,Link Phases -> Link Binary With Libraries 中没有任何内容,因为 Swift 隐式链接框架。在这种情况下,您需要在此处手动添加它并标记为可选。

您也可以只检查 UIAlertController 的可用性,而不是显式检查系统版本

if nil != NSClassFromString("UIAlertController") {
  //show alertcontroller
  ...
} else {
  //show alertview
  ...
}

【讨论】:

    【解决方案3】:

    尝试下面的 Objective C 代码。它适用于 iOS 8 及以下版本。

    if (IS_OS_8_OR_LATER) {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction
                                 actionWithTitle:@"OK"
                                 style:UIAlertActionStyleCancel
                                 handler:^(UIAlertAction *action)
                                 {
    
                                 }];
    [alertVC addAction:cancelAction];
    
    [[[[[UIApplication sharedApplication] windows] objectAtIndex:0] rootViewController] presentViewController:alertVC animated:YES completion:^{
    
    }];
    }
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 2015-09-01
      • 2023-04-04
      • 2015-12-06
      相关资源
      最近更新 更多