gpt4 book ai didi

ios - UIStoryBoard ViewControllers 是否与 "Vanilla"ViewControllers 不同?

转载 作者:行者123 更新时间:2023-11-28 15:01:23 25 4
gpt4 key购买 nike

假设我有 2 个类(class)。

1) 类 ViewControllerA: UIViewController
2) 类ViewControllerB: UIViewController

此外,我的 Main.storyboard 文件中有 2 个场景

1) 有绿色背景的场景附在 ViewControllerA 上,带有 1 个按钮
2) ViewControllerB

附加了蓝色背景的场景

我实例化B有区别吗

let newScene = ViewControllerB()
self.present(newScene, animated: true, completion: nil)

与使用 Storyboard辅助方法相反

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newScene = storyBoard.instantiateViewController(withIdentifier: "ViewControllerB") as! ViewControllerB
self.present(newScene, animated: true, completion: nil)

如果有区别,为什么不同?

最佳答案

Is there a difference between me instantiating B as opposed to using a storyboard helper method.

绝对

Storyboard为我们做了很多很棒的事情(如果设置正确的话)。但是,如果您自己实例化 View Controller ,则必须自己设置所有内容。这意味着,您必须实例化 View 、将控件事件连接到方法、配置 UI 的外观和感觉等等……所有这些都在代码中。 Storyboard将所有这些代码转换为资源文件并为我们完成大部分工作。

例如:

在 Storyboard 中,您可以将控件中的事件连接到 View Controller 中标有@IBAction 的方法。但是如果你不使用 Storyboard,那么你必须自己连接控制事件:

self.myButton.addTarget(self, action: #selector(buttonTapped:), for: .touchUpInside)

关于ios - UIStoryBoard ViewControllers 是否与 "Vanilla"ViewControllers 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48878270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com