ios 在 Swift 中实例化并呈现一个 viewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24035984/
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
Instantiate and Present a viewController in Swift
提问by E-Riddie
Issue
问题
I started taking a look of the new Swift
on Xcode 6
, and I tried some demo projects and tutorials. Now I am stuck at:
我开始查看 上的新Swift
内容Xcode 6
,并尝试了一些演示项目和教程。现在我被困在:
Instantiating and then presenting a viewController
from a specific storyboard
实例化然后呈现viewController
来自特定故事板的 a
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 上实现这一目标?
回答by akashivskyy
This answer was last revised for Swift 5.2 and iOS 13.4 SDK.
此答案最后为 Swift 5.2 和 iOS 13.4 SDK 修订。
It's all a matter of new syntax and slightly revised APIs. The underlying functionality of UIKit hasn't changed. This is true for a vast majority of iOS SDK frameworks.
这都是新语法和稍微修改的 API 的问题。UIKit 的底层功能没有改变。对于绝大多数 iOS SDK 框架来说都是如此。
let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "myVCID")
self.present(vc, animated: true)
If you're having problems with init(coder:)
, please refer to EridB's answer.
如果您遇到问题init(coder:)
,请参阅EridB 的回答。
回答by E-Riddie
For people using @akashivskyy's answerto 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
required init?(coder aDecoder: NSCoder)
在您UIViewController
尝试实例化的目的地手动实施
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
If you need more description please refer to my answer here
如果您需要更多描述,请参阅我的回答here
回答by Abhijeet
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)
}
回答by Nahuel Roldan
akashivskyy's answer works just fine! But, in case you have some trouble returning from the presented view controller, this alternative can be helpful. It worked for me!
akashivskyy 的回答工作得很好!但是,如果您在从呈现的视图控制器返回时遇到一些问题,这种替代方法可能会有所帮助。它对我有用!
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:
对象-C:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MyStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"someViewController"];
[self.navigationController showViewController:vc sender:nil];
回答by Shahzaib Maqbool
Swift 4.2 updated code is
Swift 4.2 更新的代码是
let storyboard = UIStoryboard(name: "StoryboardNameHere", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewControllerNameHere")
self.present(controller, animated: true, completion: nil)
回答by Hamid
If you want to present it modally, you should have something like bellow:
如果你想以模态方式呈现它,你应该有如下内容:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("YourViewControllerID")
self.showDetailViewController(vc as! YourViewControllerClassName, sender: self)
回答by Maxxafari
// "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 中时,这对我来说很好用
回答by vijeesh
I would like to suggest a much cleaner way. This will be useful when we have multiple storyboards
我想建议一种更清洁的方法。当我们有多个故事板时,这将很有用
1.Create a structure with all your storyboards
1.用你所有的故事板创建一个结构
struct Storyboard {
static let main = "Main"
static let login = "login"
static let profile = "profile"
static let home = "home"
}
2. Create a UIStoryboard extension like this
2. 像这样创建一个 UIStoryboard 扩展
extension UIStoryboard {
@nonobjc class var main: UIStoryboard {
return UIStoryboard(name: Storyboard.main, bundle: nil)
}
@nonobjc class var journey: UIStoryboard {
return UIStoryboard(name: Storyboard.login, bundle: nil)
}
@nonobjc class var quiz: UIStoryboard {
return UIStoryboard(name: Storyboard.profile, bundle: nil)
}
@nonobjc class var home: UIStoryboard {
return UIStoryboard(name: Storyboard.home, bundle: nil)
}
}
Give the storyboard identifier as the class name, and use the below code to instantiate
将故事板标识符作为类名,并使用以下代码进行实例化
let loginVc = UIStoryboard.login.instantiateViewController(withIdentifier: "\(LoginViewController.self)") as! LoginViewController
回答by drew..
Swift 4:
斯威夫特 4:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let yourVC: YourVC = storyboard.instantiateViewController(withIdentifier: "YourVC") as! YourVC
回答by Aks
If you have a Viewcontroller not using any storyboard/Xib, you can push to this particular VC like below call :
如果您有一个不使用任何故事板/Xib 的视图控制器,您可以推送到这个特定的 VC,如下面的调用:
let vcInstance : UIViewController = yourViewController()
self.present(vcInstance, animated: true, completion: nil)