简体   繁体   English

如何在同一容器 Swift 内的 nib 视图之间切换?

[英]How to switch between nib views inside the same container Swift?

I have a container with a few reusable elements: imageView, label and a stackView.我有一个包含一些可重用元素的容器:imageView、label 和一个 stackView。

class ContainerViewController: UIViewController {

// MARK: - Constants

var stepList = [UIViewController]()

// MARK: - Outlets
@IBOutlet private(set) weak var logoImageview: UIImageView!
@IBOutlet private(set) weak var titleLabel: UILabel!
@IBOutlet private(set) weak var progressStackView: UIStackView!

. . .
}

I also have another three viewControllers designed with Nib files, let's call them oneVC, twoVC and threeVC.我还有另外三个用 Nib 文件设计的 viewController,我们称它们为 oneVC、twoVC 和 threeVC。 This is how they look:这是他们的样子:

class StepOneViewController: UIViewController {

// MARK: - Constants
. . .

@IBOutlet private(set) weak var nextButton: UIButton!

. . .
}

Each controller has it's own nextButton, and it's own IBAction to trigger when that button is tapped.每个 controller 都有自己的 nextButton,并且在点击该按钮时触发它自己的 IBAction。 It should make a push to the next viewController, but that's not happening.它应该推送到下一个 viewController,但这并没有发生。

When you access the container, the oneVC is displayed among the reusable elements of the container.当您访问容器时,oneVC 会显示在容器的可重用元素中。 This works.这行得通。

I don't know how to access each button of my controllers from my containerController.我不知道如何从我的 containerController 访问我的控制器的每个按钮。 I need to achieve that to show the next nib, and it must be able to go back too.我需要实现这一点才能显示下一个笔尖,它也必须能够返回 go。

This code from my ContainerViewController add each controller's view as a subview:我的 ContainerViewController 中的这段代码将每个控制器的视图添加为子视图:

private func setupControllers() {
    stepList.append(StepOneViewController(nibName: "StepOneViewController", bundle: nil))
    stepList.append(StepTwoViewController(nibName: "StepTwoViewController", bundle: nil))
    stepList.append(StepThreeViewController(nibName: "StepThreeViewController", bundle: nil))

    guard let stepOneView   = stepList.first?.view,
          let stepTwoView   = stepList[1].view,
          let stepThreeView = stepList.last?.view
    else { return }
    
    configureStepViewsLayout(onStepView: stepOneView)
    
    
}

private func configureStepViewsLayout(onStepView stepView: UIView) {
    view.addSubview(stepView)
    stepView.translatesAutoresizingMaskIntoConstraints = false
    let guide = view.safeAreaLayoutGuide
    NSLayoutConstraint.activate([
        stepView.topAnchor.constraint(equalTo: progressStackView.bottomAnchor,
                                         constant: 20),
        stepView.leftAnchor.constraint(equalTo: guide.leftAnchor),
        stepView.rightAnchor.constraint(equalTo: guide.rightAnchor),
        stepView.bottomAnchor.constraint(equalTo: guide.bottomAnchor)
    ])
}

I also need to change the progressStackView color every time any nextButton is tapped and shows the next controller.每次点击任何 nextButton 并显示下一个 controller 时,我还需要更改 progressStackView 颜色。

What you're doing is totally illegal.你的所作所为是完全违法的。 You cannot simply say你不能简单地说

view.addSubview(stepView)

out of the blue like that.像那样出乎意料。 You are effectively just using the nib as a form of dumpster diving;您实际上只是将笔尖用作垃圾箱潜水的一种形式; you are grabbing the view and shoving it into your interface, but the view controller (eg StepOneViewController) bears no formal relationship to your view controller (ContainerViewController).您正在抓取视图并将其推入您的界面,但视图 controller(例如 StepOneViewController)与您的视图 controller(ContainerViewController)没有正式关系。 You are using the word "container", but this is not a container view controller.您使用的是“容器”一词,但这不是容器视图 controller。

There is an elaborate parent–child dance that you must do in order to get the child (eg StepOneViewController) into the view controller hierarchy and make your container view controller be a container view controller — and you are not doing the dance.为了让孩子(例如 StepOneViewController)进入视图 controller 层次结构并使您的容器视图 controller成为容器视图 Z594C103F2C6E01C3D8AB059F,您必须进行精心制作的父子舞蹈。

Once you do the dance, the child will be able to talk to the parent as its parent .一旦你跳了舞,孩子就可以像父母一样与parent交谈。 The parent can then remove the child (again using a formal dance) and replace it with another.然后父母可以移除孩子(再次使用正式的舞蹈)并用另一个替换它。

Also, it's a really bad idea to retain three view controllers as you are doing.此外,保留三个视图控制器是一个非常糟糕的主意。 You only have one visible child view at a time so you only need one child view controller at any given moment.您一次只有一个可见的子视图,因此在任何给定时刻您只需要一个子视图 controller。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM