Xcode - 如何以编程方式在容器视图中嵌入/更改视图控制器? [英] Xcode - How can i programmatically embed/change view controller within a container view?
问题描述
我正在尝试以编程方式设置/或更改控制器视图内的嵌入式视图控制器.
I am trying to programmatically set / or change the embedded View Controller inside a Controller View.
我有 3 个不同的视图控制器,我想在容器视图中显示它们,所有这些都取决于用户是否登录.
I have 3 different View Controllers, that I would like to show in the Container View, all depending on if the user is logged in or not.
我环顾四周并尝试了一堆代码,我发现一个有效,但代码改变了自我视图,而不是视图容器视图.
I have looked around and tried a bunch of code, I found one that worked, but the code changed the self view, and not the view containers view.
我尝试过的很多代码也没有在 Swift 3 中使用,所以作为一个新的应用程序开发人员,这压力很大,因为我试图将其转换为 Swift 3.
A lot of the code I have tried have also not been in Swift 3, so as a new app developer, this has been quite stressful, as I tried to convert it to Swift 3.
任何人都可以提供更改视图容器内的嵌入式视图控制器的解决方案吗?谢谢.
Can anyone provide a solution for changing the embedded view controller inside a view container? Thanks.
推荐答案
我可能已经找到了解决方案.我在这里回答,以防它可以帮助我的情况下的任何其他人.
I might have found a solution for this. I'm answering here, in case it can help anyone else in my situation.
我所做的是添加一个新的视图控制器,然后将其嵌入视图容器 - 这将用作主视图" - 然后我使用这个空白视图控制器来决定在空白的 self 内应该更改哪个其他视图控制器.
What I did was add a new View Controller and then embed it to the View Container - This will work as a "master view" - I then use this blank view controller to decide which other view controller should be changed within the self of the blank.
这是我在空白视图控制器中的一些代码,但我想空白视图控制器也可以用作主视图控制器(在我的情况下为帐户"),然后它可以添加登录/注册视图小时候.
Here's some code I have in the blank view controller, but I suppose the blank view controller can also be used as a master view controller (in my case for "Account"), and then it can add the login/register view as a child.
override func viewDidLoad() {
super.viewDidLoad()
updateView()
}
private lazy var loginViewController: loginViewController = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
// Instantiate View Controller
var viewController = storyboard.instantiateViewController(withIdentifier: "loginViewController") as! loginViewController
// Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
private lazy var registerViewController: registerViewController = {
// Load Storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
// Instantiate View Controller
var viewController = storyboard.instantiateViewController(withIdentifier: "registerViewController") as! registerViewController
// Add View Controller as Child View Controller
self.add(asChildViewController: viewController)
return viewController
}()
private func add(asChildViewController viewController: UIViewController) {
// Add Child View Controller
addChildViewController(viewController)
// Add Child View as Subview
view.addSubview(viewController.view)
// Configure Child View
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Notify Child View Controller
viewController.didMove(toParentViewController: self)
}
private func remove(asChildViewController viewController: UIViewController) {
// Notify Child View Controller
viewController.willMove(toParentViewController: nil)
// Remove Child View From Superview
viewController.view.removeFromSuperview()
// Notify Child View Controller
viewController.removeFromParentViewController()
}
private func updateView() {
add(asChildViewController: registerViewController)
/*
if segmentedControl.selectedSegmentIndex == 0 {
remove(asChildViewController: sessionsViewController)
add(asChildViewController: summaryViewController)
} else {
remove(asChildViewController: summaryViewController)
add(asChildViewController: sessionsViewController)
}
*/
}
感谢这个人:https://cocoacasts.com/管理视图控制器与容器视图控制器/
这篇关于Xcode - 如何以编程方式在容器视图中嵌入/更改视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!