在Swift中实例化并呈现一个视图控制器

351

问题

我开始学习 Swift 编程语言,但是在从特定的 UIStoryboard 初始化 UIViewController 的时候,我无法正确地进行输入。

Objective-C 中,我只需要写:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"ViewControllerID"];
[self presentViewController:viewController animated:YES completion:nil];

有谁可以帮我学习如何在Swift中实现这个功能吗?

18个回答

3

Swift 3 Storyboard

let settingStoryboard : UIStoryboard = UIStoryboard(name: "SettingViewController", bundle: nil)
let settingVC = settingStoryboard.instantiateViewController(withIdentifier: "SettingViewController") as! SettingViewController
self.present(settingVC, animated: true, completion: {

})

2
如果您有一个没有使用任何故事板/Xib的Viewcontroller,您可以像下面这样调用来推送到这个特定的VC:
 let vcInstance : UIViewController   = yourViewController()
 self.present(vcInstance, animated: true, completion: nil)

我得到了这个错误信息:"无法构造,因为它没有可访问的初始化器"。 - Paul Bénéteau
故事板创建的插座和其他关系已经丢失。 - jose920405

1

Swift 5

let vc = self.storyboard!.instantiateViewController(withIdentifier: "CVIdentifier")
self.present(vc, animated: true, completion: nil)

1
我知道这是一个旧的线程,但我认为当前的解决方案(使用硬编码的字符串标识符来给定视图控制器)非常容易出错。
我已经创建了一个构建时脚本(可以在这里访问),它将创建一个编译器安全的方式,用于从给定项目中的所有故事板中访问和实例化视图控制器。
例如,在Main.storyboard中命名为vc1的视图控制器将被实例化如下:
let vc: UIViewController = R.storyboard.Main.vc1^  // where the '^' character initialize the controller

0
if let destinationVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationVC") as? DestinationVC{
            let nav = self.navigationController
            //presenting
            nav?.present(destinationVC, animated: true, completion: {
                
            })
            //push
            nav?.pushViewController(destinationVC, animated: true)
        }

你的回答可以通过提供更多支持信息来改进。请添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心找到有关如何编写良好答案的更多信息。 - Saeid Amini

0
我创建了一个库,可以更轻松地处理这个问题,并具有更好的语法:

https://github.com/Jasperav/Storyboardable

只需更改Storyboard.swift文件,让ViewControllers符合Storyboardable协议即可。


0

我使用这个帮助程序:

struct Storyboard<T: UIViewController> {
    
    static var storyboardName: String {
        return String(describing: T.self)
    }
    
    static var viewController: T {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        
        guard let vc = storyboard.instantiateViewController(withIdentifier: Self.storyboardName) as? T else {
            fatalError("Could not get controller from Storyboard: \(Self.storyboardName)")
        }
        
        return vc
    }
}

使用方法(Storyboard ID 必须与 UIViewController 类名匹配)

let myVC = Storyboard.viewController as MyViewController

0
guard let vc = storyboard?.instantiateViewController(withIdentifier: "add") else { return }
        vc.modalPresentationStyle = .fullScreen
        present(vc, animated: true, completion: nil)

1
请在您的答案中添加一些解释。解释底层逻辑比仅仅给出代码更重要。这有助于作者和其他读者自行修复此类问题,同时为他们提供扩展编程技能所需的知识线索。 - Ironkey

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