Xcode-如何以编程方式在容器视图中嵌入/更改视图控制器? [英] Xcode - How can i programmatically embed/change view controller within a container view?
问题描述
我正在尝试以编程方式设置/或更改 Controller View 中的嵌入式 View Controller .
I am trying to programmatically set / or change the embedded View Controller inside a Controller View.
我有3种不同的View Controller,我想在Container View中显示它们,这取决于用户是否登录.
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.
我环顾四周并尝试了一堆代码,发现一个可行的代码,但是代码更改了self视图,而不是view container视图.
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.
我所做的是添加一个新的 View Controller ,然后将其嵌入到 View容器中-这将作为主视图"工作-然后我使用此空白视图控制器来决定应在空白区域内更改哪个其他视图控制器.
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屋!