可以在iOS 7和iOS 8上运行的警报(Alert)

11

当我尝试运行这个庞然大物时,出现了dyld: Symbol not found: _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标记为可选项而不是必需项。现在的简化版本是:

已解决:必须将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)
}

可能是在Swift中显示UIAlertView的错误的重复问题。 - dibi
1
这是你所提到的问题的超集。其他人建议使用以下代码: let alert = UIAlertView() alert.title = "Noop" alert.message = "Nothing to verify" alert.addButtonWithTitle("Click") alert.show()这对我有效。有人应该向苹果报告一个关于UIAlertView shim中方便初始化损坏的错误。 - Anton Tropashko
请查看我在这里发布的答案:https://dev59.com/xYDba4cB1Zd3GeqPD2c6#24091779 - Guy Kahlon
我做了。所提到的条目与UIAlertView初始化有关。我的条目与既拥有蛋糕又吃掉它有关。 - Anton Tropashko
"UIKit 必须标记为 Optional 而不是 Required 是什么意思?" - Bellots
链接阶段 -> 链接二进制文件与库,有硬链接和软链接开关(每个框架都有),将其从必需更改为可选(对于UIKit),以便该iOS不可知代码在动态链接器中加载时不会出错。 - Anton Tropashko
3个回答

5
  • Explicitly add UIKit in the "Link Binary With Libraries" section of your project's build phases.

  • You can test for the existence of UIAlertController like this:

    if NSClassFromString("UIAlertController") != nil {
        // Use it
    } else {
        // Fall back
    }
    
  • I wrote a wrapper that works on both iOS 7 and iOS 8. You can find it here. It takes a view controller followed by a bunch of optional arguments and any number of buttons:

    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中使用它。 - Arnlee Vizcayno
这看起来很酷。但是,虽然它在iOS8上完美运行,在iOS7上它显示了预期的警报视图,但没有调用两个按钮的完成处理程序。我可能做错了什么?showAlert(tabBarController,style:.Alert,title:“需要登录”,message:“在使用此功能之前,您必须先登录。”,sourceView:nil,completion:nil, (.Cancel,“取消”,{println(“ko”)}), (.Default,“登录”,{ println(“ok”) })) - devguy
我尝试复制/粘贴您的示例,但即使在iOS7上也无法正常工作。 - devguy

3
在Xcode 6 beta 5中,由于Swift隐式链接框架,所以在"Link Phases -> Link Binary With Libraries"中没有任何内容。 在这种情况下,您需要手动添加它,并将其标记为可选项。
此外,您可以仅检查UIAlertController的可用性,而无需明确检查系统版本。
if nil != NSClassFromString("UIAlertController") {
  //show alertcontroller
  ...
} else {
  //show alertview
  ...
}

-1
尝试下面的 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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接