为什么 InstantiateViewContoller 是必要的? [英] Why instantiateViewContoller is necessary?

查看:35
本文介绍了为什么 InstantiateViewContoller 是必要的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能使用诸如此类的方法创建视图控制器对象resultVC = ResultViewController() 而不是下面的方式.

Why can't I create the view controller objects using sth like resultVC = ResultViewController() instead of the following way.

        let storyboard = UIStoryboard (name: "Main", bundle: nil)
        let resultVC = storyboard.instantiateViewController(withIdentifier: "ResultViewController") as! ResultViewController

        // Communicate the match
        resultVC.match = self.match
        self.navigationController?.pushViewController(resultVC, animated: true)

推荐答案

一切都取决于你的逻辑.您可以通过三种基本方式创建 UIViewController.

Everything is depends on your logic. There are three basic ways by which you can create UIViewController.

  1. 故事板:你有故事板,设计你的 VC 并通过故事板实例化它.在这种情况下,您必须告诉系统您的 VC 在哪个故事板中以及它的 ID 是什么.正如您在上面的代码中所做的那样.

  1. Storyboard : you have storyboard, design your VC and instantiate it via storyboard. In this case, you have to tell system in which storyboard your VC have and what is its ID. As you done in above code.

Xib/Nib:与故事板一样,您可以使用 xib/nib 来设计您的 VC.这里只需要按 xib 名称分配 VC.

Xib/Nib: Like storyboard, you can use xib/nib and design your VC. Here just you need to alloc the VC by the xib name.

以编程方式:在这里您不需要任何类型的 xib/故事板.您必须通过代码来完成所有工作.您的 VC 设计将在您各自的 VC 文件中.在这里,您只需分配该文件.

Programatically: Here you donot need any type of xib/ storyboard. You have to do everything by code. your VC design will be in your respective VC file. Here you have to just alloc that file.

区别: 哪个更高效道路?故事板还是 XIB?

如果你还不清楚,那就问吧.

If you still unclear, then ask.

这篇关于为什么 InstantiateViewContoller 是必要的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆