xcode showViewController 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26944432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:10:46  来源:igfitidea点击:

showViewController Not Working

iosxcodeswiftxcode6

提问by John Doe

I am developing an app in SWIFT and I am using the following code to show a view controller on a button click.

我正在 SWIFT 中开发一个应用程序,我正在使用以下代码在单击按钮时显示视图控制器。

let storyboard = UIStoryboard(name: "Main", bundle: nil);
var viewName:NSString = "websiteView"
let vc = storyboard.instantiateViewControllerWithIdentifier(viewName) as WebsiteViewController
self.showViewController(vc, sender: self)

it works perfectly when I test it for ios 8 but on ios 7 no matter what I do I get the following error message. I read on a forum that self.showViewController was only available for ios 8 but the compiler doesn't throw an error, does anyone know if I can use it?

当我为 ios 8 测试它时,它运行良好,但在 ios 7 上,无论我做什么,我都会收到以下错误消息。我在论坛上读到 self.showViewController 仅适用于 ios 8 但编译器不会抛出错误,有人知道我是否可以使用它吗?

showViewController:sender:]: unrecognized selector sent to instance 0x7f9552e7cc50

2014-11-15 09:25:49.565 Throw[80595:613] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Throw.test showViewController:sender:]: unrecognized selector sent to instance 0x7f9552e7cc50'

2014-11-15 09:25:49.565 Throw[80595:613] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Throw.test showViewController:sender:]:无法识别的选择器发送到实例 0x7f95052e

回答by Acey

The compiler didn't throw an error because you have the iOS 8.0 (or 8.1) SDK selected. That method is indeed iOS 8.0+ only. You can choose to use it if you call it conditionally, like so:

编译器没有抛出错误,因为您选择了 iOS 8.0(或 8.1)SDK。该方法确实仅适用于 iOS 8.0+。如果您有条件地调用它,您可以选择使用它,如下所示:

if (self.respondsToSelector(Selector("showViewController"))) {
    self.showViewController(vc, sender: self)
} else {
    //An example of the alternate if you are using navigation controller
    self.navigationController?.pushViewController(vc, animated: true)
}

回答by f0go

You can use:

您可以使用:

self.navigationController?.pushViewController(vc as WebsiteViewController, animated: true)

which is compatible with iOS 7 and iOS 8

与 iOS 7 和 iOS 8 兼容

回答by kaushal

Update - A better and recommended way from swift2 for API version compatibility check

更新 - 来自 swift2 的更好和推荐的 API 版本兼容性检查方法

if #available(iOS 8, *) {
   self.showViewController(vc, sender: self)
} else {
  self.navigationController?.pushViewController(vc, animated: true)
}

Bonus : Use the @available attribute to decorate individual functions or entire classes this will give compile time version check for your own API

奖励:使用@available 属性来装饰单个函数或整个类,这将为您自己的 API 提供编译时版本检查

@available(iOS 8, *)
func compatibalityCheckForiOS8() -> Void { }