Ios 在Swift中实例化并显示viewController 问题
我开始在Xcode 6上查看新的Swift,并尝试了一些演示项目和教程。现在我被困在: 从特定情节提要实例化并呈现viewController 目标C溶液 如何在Swift上实现这一点?此答案上次是针对Swift 5.4和iOS 14.5 SDK修订的 这都是新语法和稍加修改的API的问题。UIKit的基本功能没有改变。绝大多数iOS SDK框架都是如此 let storyboardname:myStoryboardName,bundle:nil 让vc=storyboard.instanceeviewcontrollerwhiteIdentifier:myVCID self.presentvc,动画:true 确保在故事板内部的“故事板ID”下设置myVCID 对于使用来实例化UIViewController的用户,存在以下异常: 致命错误:对类使用未实现的初始值设定项“initcoder:” 快速提示: 在您试图实例化的目标UIViewController上手动实现所需的init?coder aDecoder:NSCoderIos 在Swift中实例化并显示viewController 问题,ios,objective-c,swift,uiviewcontroller,Ios,Objective C,Swift,Uiviewcontroller,我开始在Xcode 6上查看新的Swift,并尝试了一些演示项目和教程。现在我被困在: 从特定情节提要实例化并呈现viewController 目标C溶液 如何在Swift上实现这一点?此答案上次是针对Swift 5.4和iOS 14.5 SDK修订的 这都是新语法和稍加修改的API的问题。UIKit的基本功能没有改变。绝大多数iOS SDK框架都是如此 let storyboardname:myStoryboardName,bundle:nil 让vc=storyboard.instancee
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
let loginVc = UIStoryboard.login.instantiateViewController(withIdentifier: "\(LoginViewController.self)") as! LoginViewController
如果你需要更多的描述,请参考我的答案
当我把它放在AppDelegate中时,它对我很有效
斯威夫特:
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
self.presentViewController(viewController, animated: false, completion: nil)
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.showViewController(vc, sender: nil)
目标C
UIViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];
阿卡西夫斯基的回答很好!但是,如果从显示的视图控制器返回时遇到一些问题,此替代方案可能会有所帮助。这对我有用 斯威夫特:
let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
self.presentViewController(viewController, animated: false, completion: nil)
let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.showViewController(vc, sender: nil)
Obj-C:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"someViewController"];
[self.navigationController showViewController:vc sender:nil];
如果你想以一种方式展示它,你应该像下面这样:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("YourViewControllerID")
self.showDetailViewController(vc as! YourViewControllerClassName, sender: self)
我知道这是一个旧线程,但我认为当前的解决方案是为给定的视图控制器使用硬编码字符串标识符,这很容易出错 我已经创建了一个构建时脚本,您可以使用它创建一种编译器安全的方式,用于从给定项目中的所有情节提要访问和实例化视图控制器 例如,Main.storyboard中名为vc1的视图控制器的实例化如下:
let vc: UIViewController = R.storyboard.Main.vc1^ // where the '^' character initialize the controller
如果您的Viewcontroller不使用任何storyboard/Xib,您可以按如下方式调用此特定VC:
let vcInstance : UIViewController = yourViewController()
self.present(vcInstance, animated: true, completion: nil)
斯威夫特3
无论我尝试了什么,它都不适合我——没有错误,但我的屏幕上也没有新的视图控制器。不知道为什么,但将其包装在timeout函数中最终使其工作:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.0) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TabletViewController")
self.present(controller, animated: true, completion: nil)
}
Swift 4:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC: YourVC = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
我想建议一种更干净的方法。当我们有多个故事板时,这将非常有用 1.创建一个包含所有故事板的结构 二,。像这样创建UIStoryboard扩展 将情节提要标识符作为类名,并使用下面的代码进行实例化
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
let loginVc = UIStoryboard.login.instantiateViewController(withIdentifier: "\(LoginViewController.self)") as! LoginViewController
我创建了一个库,使用更好的语法可以更轻松地处理此问题:
只需更改Storyboard.swift并使ViewController符合Storyboard的要求。swift 4.2更新的代码是
let storyboard = UIStoryboard(name: "StoryboardNameHere", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerNameHere")
self.present(controller, animated: true, completion: nil)
斯威夫特5
let vc = self.storyboard!.instantiateViewController(withIdentifier: "CVIdentifier")
self.present(vc, animated: true, completion: nil)
您甚至可以省略;您介意详细介绍一下作为UIViewController的应用程序吗?为什么这是必要的?因为关键字用于类型转换。这与UIViewController*objective cYeah中的一个对象相同,我知道我可以省略它们,但这是一个冗长习惯的一部分:D作为实例化的EviewControllerWithiIdentifier返回Swift中等效的任何对象id,我声明vc为UIViewController,我必须将任何对象类型转换为UIViewController。朋友们,我面临一些不同的问题,这段代码在模拟器上运行,而不是在iOS v7.1.2的ipad2上运行。“如果你不介意的话,帮我解决这个问题。”阿卡西夫斯基非常肯定地对你说。但可能不是对某些人。如果你想使用自定义启动器,你可以用super.initnibName:nil、bundle:nil调用super,whew将很好地加载,或者用NIB文件的名称调用它。@user1700737我正在实例化viewController,它引用脚本,脚本执行initWithCoder,而不是initWithNib。参考这个问题嗨,朋友们,我面临着一些不同的问题,这段代码运行在模拟器上,而不是在我的ipad2上,它有iOS v7.1.2。如果你不介意帮我解决这个问题。@Indra你需要在stack上问另一个问题,给我链接,我很乐意帮助你!在Swift 1.2上,您必须使initcoder成为aDecoder:NSCoder!必修的。现在您必须编写以下代码:必需的initcoder aDecoder:NSCoder!我明白了:无法构造,因为它没有可访问的初始值设定项。故事板生成的输出和其他关系丢失了@nonobjc for?@StackRunner这将不可用于目标,请对您的答案添加一些解释。解释底层逻辑比给出代码更重要。它帮助作者和其他读者自己解决这个问题和类似的问题,同时为他们提供扩展编程技能所需的知识线索。
let storyboard = UIStoryboard(name: "StoryboardNameHere", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerNameHere")
self.present(controller, animated: true, completion: nil)
let vc = self.storyboard!.instantiateViewController(withIdentifier: "CVIdentifier")
self.present(vc, animated: true, completion: nil)
guard let vc = storyboard?.instantiateViewController(withIdentifier: "add") else { return }
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true, completion: nil)