在Swift中动态创建uiviewcontroller [英] Creating uiviewcontroller dynamically in Swift
问题描述
我想动态创建UIViewController而不创建类或使用Mainstoryboard。我希望它以编程方式发生。是否可能?
我想创建UIViewControllers,具体取决于动态数据。
是的,您可以通过编程方式动态创建视图。我知道你说你不想上课,但你真的必须这样做。但关键是,你不必让这个类硬编码UI,而是动态构建它。
所以,你可以继承 UIViewController
,但传递它动态构建视图所需的数据,然后实现
如您所见,其中一个路径(自定义 loadView
方法)绕过故事板/ NIB工作流程。注意,如果你进行这个过程,你负责实例化 UIView
对象并设置视图
属性。查看控制器。另外,在您的实现中 not 调用 super.loadView()
。
For例如,如果我想在视图中添加一堆 UILabel
对象,可以在Swift 3中执行以下操作:
class DynamicViewController:UIViewController {
var strings:[String]!
覆盖func loadView(){
// super.loadView()//不要调用SUPER
view = UIView()
view。 backgroundColor = .lightGray
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
view.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.centerXAnchor.constraint(equalTo:view.centerXAnchor),
stackView.centerYAnchor.constraint(equalTo:view.centerYAnchor)
])
表示字符串中的字符串{
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = string
stackView.addArrangedSubview(label)
}
}
}
你可以像所以:
@IBAction func di dTapButton(_ sender:AnyObject){
let controller = ViewController()
controller.strings = [Hello world,Foobar,Baz]
show(controller,sender:发件人)
}
(对于Swift 2再现,请参阅此答案的前一版本。)
话虽如此,我不确定你为什么不关心不使用NIB或故事板。只需使用空白的。然后,您可以再次以编程方式动态地将控件添加到场景中,但是在 viewDidLoad
中执行此操作。它实现了与上面完全相同的效果,但避免使用相当陈旧的 loadView
技术,并且必须实例化并加载视图
属性你自己。
I want to create UIViewController dynamically without creating a class or using Mainstoryboard.I want it to happen programatically.Is it possible? I want to create UIViewControllers depending on the dynamic data.
Yes, you can create a view dynamically, programmatically. I know you said you don't want to use a class, but you really have to. The key, though, is that you don't have to have this class hard code the UI, but rather build it dynamically.
So, you can subclass UIViewController
, but pass it the data it needs to build the view dynamically and then implement the loadView
method to do so. No NIB or storyboard scene is needed. Just create the view
and build the subviews based upon your dynamic data. This is described in the legacy View Controller Programming Guide. (Please note that portions of that document no longer apply, notably al of the "unload" view discussion. But it does describe and illustrate the loadView
process, which still works fine.)
The process for building a view for a view controller is as follows:
As you can see, one of the paths (the custom loadView
method) bypasses the storyboard/NIB workflow. Note, if you go about this process, you are responsible for instantiating a UIView
object and setting the view
property of the view controller. Also, do not call super.loadView()
in your implementation.
For example, if I wanted to just add a bunch of UILabel
objects to the view, in Swift 3 you could do something like:
class DynamicViewController: UIViewController {
var strings: [String]!
override func loadView() {
// super.loadView() // DO NOT CALL SUPER
view = UIView()
view.backgroundColor = .lightGray
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .vertical
view.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
for string in strings {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = string
stackView.addArrangedSubview(label)
}
}
}
And you could present it like so:
@IBAction func didTapButton(_ sender: AnyObject) {
let controller = ViewController()
controller.strings = ["Hello world", "Foobar", "Baz"]
show(controller, sender: sender)
}
(For Swift 2 renditions, see previous revision of this answer.)
Having said this, I'm not sure why you're concerned about not using a NIB or storyboard. Just use a blank one. And then, you can again programmatically add your controls to the scene dynamically, but do it in viewDidLoad
. It achieves the exact same effect as the above, but avoids using the fairly antiquated loadView
technique and from having to instantiate and load the view
property yourself.
这篇关于在Swift中动态创建uiviewcontroller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!