I have a main view controller, inside this view controller I show another view controller as its child view controller. The code to show the child view controller as shown below. The self.currentController is the child controller which will be located inside the main controller.
我有一个主视图控制器,在这个视图控制器中我显示另一个视图控制器作为它的子视图控制器。显示子视图控制器的代码如下所示。自我。currentController是将位于主控制器中的子控制器。
self.addChildViewController(self.currentController!)
self.currentController?.view.frame = self.operationView.bounds
self.currentController?.view.layoutIfNeeded()
self.operationView.addSubview((self.currentController?.view!)!)
self.setNeedsStatusBarAppearanceUpdate()
Now I want to perform a show segue (another controller, let call it ThirdController) inside the child view controller by using below code:
现在我想在子视图控制器中执行一个show segue(另一个控制器,我们称它为ThirdController),使用下面的代码:
performSegueWithIdentifier("ShowSegue", sender: nil)
on doing this, the ThirdController will fill on the full screen. What I want to do is to show the third controller on the child controller place. How can I do this?
在此过程中,ThirdController将填充整个屏幕。我要做的是显示子控制器位置上的第三个控制器。我该怎么做呢?
2 个解决方案
#1
1
OK, Sorry I don't know how to write the answer in Swift. So I'll just show you how my solution is done in Objective-C.
好的,对不起,我不知道怎么用Swift写答案。我将向你们展示如何用Objective-C求解。
Code to load the first subview:
加载第一个子视图的代码:
- (void)loadASubview
{
subview = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];
[self addChildViewController:subview];
[self.view addSubview:subview.view];
[subview didMoveToParentViewController:self];
[subview.view setFrame:self.view.bounds];
}
Code to unload the subview:
卸载子视图的代码:
- (void)unloadASubview
{
[subview willMoveToParentViewController:nil];
[subview.view removeFromSuperview];
[subview removeFromParentViewController];
}
Initially, when I need to load subview A, I will simply call loadASubview. After that, if I need to load another subview, I will unload the subview I previously loaded by calling unloadASubview before loading the new subview.
最初,当我需要加载子视图A时,我只需调用loadASubview。之后,如果我需要加载另一个子视图,我将在加载新子视图之前调用unloadASubview卸载之前加载的子视图。
Please take note that the "subview" variable inside the functions are declared outside.
请注意,函数内的“子视图”变量在外部声明。
I hope this will help you.
我希望这能对你有所帮助。
#2
0
Swift 4
斯威夫特4
func loadASubView(){
subview = self.storyboard?.instantiateViewController(withIdentifier: "childviewstoryboardid")
self.addChildViewController(subview!)
self.containerView.addSubview(subview!.view)
subview?.didMove(toParentViewController: self)
subview?.view.frame = self.containerView.frame
}
func unloadASubview(){
subview?.willMove(toParentViewController: nil)
subview?.view.removeFromSuperview()
subview?.removeFromParentViewController()
}
转载于:https://blog.51cto.com/13852301/2324929