[英]viewDidAppear being called strangley
I have a viewController
containing segmentedControl
. 我有一个包含segmentedControl
的viewController
。 I have a VCA and VCB which are in the segmentedControl
. 我有一个VCA和VCB在segmentedControl
。 When I tap on second segment VCB appears. 当我点击第二段时,出现VCB。 Now I am pushing another ViewController from VCB. 现在,我正在从VCB推送另一个ViewController。 But when coming back from that viewController, viewDidAppear
of VCA is being Called. 但是,当从该viewController返回时,将调用VCA的viewDidAppear
。 Which is strange to me. 这对我来说很奇怪。 Because user is on the VCB so why the viewWillAppear
and viewDidAppear
of VCA are being called ? 因为用户在VCB上,所以为什么要调用VCA的viewWillAppear
和viewDidAppear
? Here is a diagram to explain more 这是一个解释更多的图
This is how I am adding viewControllers to segmentedControl 这就是我将viewControllers添加到segmentedControl的方式
func switchToViewController(viewController: UIViewController, selectedIndex: Int) {
viewController.removeFromParentViewController()
viewController.view.removeFromSuperview()
addChildViewController(viewController)
viewController.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(viewController.view)
// Setting constraints of the container view
NSLayoutConstraint.activate([
viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
])
viewController.didMove(toParentViewController: self)
}
I am just unable to understand the behavior. 我只是无法理解行为。 So please guide me. 所以请指导我。
You are never removing the current view controller and its view from the hierarchy... 您永远不会从层次结构中删除当前的视图控制器及其视图。
You need to keep track of which VC/view is currently displayed - perhaps with a currentVC
variable, and your function should look something like this: 您需要跟踪当前显示的VC /视图-可能带有currentVC
变量,并且您的函数应如下所示:
func switchToViewController(viewController: UIViewController, selectedIndex: Int) {
// remove current ViewController from VC hierarchy
currentVC.removeFromParentViewController()
// remove current VC.View from View hierarchy
currentVC.view.removeFromSuperview()
// the "incoming" ViewController becomes the "current" ViewController
currentVC = viewController
addChildViewController(viewController)
viewController.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(viewController.view)
// Setting constraints of the container view
NSLayoutConstraint.activate([
viewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
viewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),
viewController.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 50),
viewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
])
viewController.didMove(toParentViewController: self)
}
Here you go, you can achieve this by using containerView
. 在这里,您可以使用containerView
实现此目的。 ContainerView
is a normal UIView
subclass. ContainerView
是普通的UIView
子类。 Your UI will be like this. 您的用户界面将是这样。 You will have one baseViewController
where you will have segmentControl
and containerView
view in baseViewController. 您将拥有一个baseViewController
,在baseViewController中将具有segmentControl
和containerView
视图。
Assume you have two view controller namely viewController1
and viewController2
. 假设您有两个视图控制器,分别是viewController1
和viewController2
。 You can add these viewControllers as childViewController
to this containerView
like below. 您可以将以下viewControllers作为childViewController
添加到此containerView
如下所示。
import UIKit
class BaseViewController: UIViewController {
@IBOutlet weak var typeSegment: UISegmentedControl!
@IBOutlet weak var containerView: UIView!
var viewController1: UIViewController?
var viewController2: UIViewController?
// MARK: - Action method.
@IBAction func segmentIndexChanged(_ sender: Any) {
let selectedIndex = typeSegment.selectedSegmentIndex
switch selectedIndex {
case 0:
addVC1()
case 1:
addVC2()
default:
break
}
}
func rectForChildVC() -> CGRect {
let rect = CGRect(x: containerView.frame.origin.x , y: containerView.frame.origin.y, width: containerView.frame.size.width, height: containerView.frame.size.height)
return rect
}
func addVC1() {
removeVC2()
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
viewController1 = storyboard.instantiateViewController(withIdentifier: "Viewcontroller1Identifier") // Create you first view controller instance here.
viewController1?.view.frame = rectForChildVC()
addChildViewController(viewController1!)
view.addSubview((viewController1?.view)!)
viewController1?.didMove(toParentViewController: self)
view.layoutIfNeeded()
}
func addVC2() {
removeVC1()
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
viewController2 = storyboard.instantiateViewController(withIdentifier: "Viewcontroller2Identifier") // Create you second view controller instance here.
viewController2?.view.frame = rectForChildVC()
addChildViewController(viewController2!)
view.addSubview((viewController2?.view)!)
viewController2?.didMove(toParentViewController: self)
view.layoutIfNeeded()
}
func removeVC1() { // Remove first view controller.
if let viewController = viewController1 {
viewController.didMove(toParentViewController: nil)
viewController.view.removeFromSuperview()
viewController.removeFromParentViewController()
}
}
func removeVC2() { // Remove second view controller
if let viewController = viewController2 {
viewController.didMove(toParentViewController: nil)
viewController.view.removeFromSuperview()
viewController.removeFromParentViewController()
}
}
}
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.