在Swift中实例化和呈现viewController

本文探讨了在Swift中如何从特定Storyboard实例化并呈现UIViewController,引用了Objective-C的解决方案,并提供了在Swift中实现该操作的代码示例,同时解决了可能出现的'uninitialized initializer 'init(coder:)'错误的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Instantiate and Present a viewController in Swift

Issue 问题

I started taking a look of the new Swift on Xcode 6 , and I tried some demo projects and tutorials. 我开始看看Xcode 6上的新Swift ,我尝试了一些演示项目和教程。 Now I am stuck at: 现在我被困在:

Instantiating and then presenting a viewController from a specific storyboard 实例化然后从特定的故事板呈现viewController

Objective-C Solution Objective-C解决方案

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"myVCID"];
[self presentViewController:vc animated:YES completion:nil];

How to achieve this on Swift? 如何在Swift上实现这一目标?


#1楼

参考:https://stackoom.com/question/1CQRa/在Swift中实例化和呈现viewController


#2楼

It all is a matter of the new syntax, functionality hasn't changed: 这一切都是新语法的问题,功能没有改变:

// Swift 3.0

let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "someViewController")
self.present(controller, animated: true, completion: nil)

If you're having problems with init(coder:) , please refer to EridB's answer . 如果您遇到init(coder:) ,请参考EridB的答案


#3楼

For people using @akashivskyy's answer to instantiate UIViewController and are having the exception: 对于使用@ akashivskyy的答案来实例化UIViewController并且有异常的人:

fatal error: use of unimplemented initializer 'init(coder:)' for class 致命错误:在课堂上使用未实现的初始化程序'init(coder :)'

Quick tip: 小建议:

Manually implement required init?(coder aDecoder: NSCoder) at your destination UIViewController that you are trying to instantiate 在您尝试实例化的目标UIViewController上手动实现required init?(coder aDecoder: NSCoder)

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

If you need more description please refer to my answer here 如果你需要更多的描述,请参阅我的答案在这里


#4楼

// "Main" is name of .storybord file "
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
// "MiniGameView" is the ID given to the ViewController in the interfacebuilder
// MiniGameViewController is the CLASS name of the ViewController.swift file acosiated to the ViewController
var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("MiniGameView") as MiniGameViewController
var rootViewController = self.window!.rootViewController
rootViewController?.presentViewController(setViewController, animated: false, completion: nil)

This worked fine for me when i put it in AppDelegate 当我把它放在AppDelegate中时,这对我来说很好


#5楼

This link has both the implementations: 此链接具有以下两种实现:

Swift: 迅速:

let viewController:UIViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("ViewController") as UIViewController
self.presentViewController(viewController, animated: false, completion: nil)

Objective C 目标C.

UIViewController *viewController = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];

This link has code for initiating viewcontroller in the same storyboard 此链接具有用于在同一故事板中启动viewcontroller的代码

/*
 Helper to Switch the View based on StoryBoard
 @param StoryBoard ID  as String
*/
func switchToViewController(identifier: String) {
    let viewController = self.storyboard?.instantiateViewControllerWithIdentifier(identifier) as! UIViewController
    self.navigationController?.setViewControllers([viewController], animated: false)

}

#6楼

akashivskyy's answer works just fine! akashivskyy的回答很好! But, in case you have some trouble returning from the presented view controller, this alternative can be helpful. 但是,如果您从显示的视图控制器返回时遇到一些问题,则此替代方法可能会有所帮助。 It worked for me! 它对我有用!

Swift: 迅速:

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: OBJ-C:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"someViewController"];
[self.navigationController showViewController:vc sender:nil];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值