首页 > 解决方案 > 更改 UISegmentedControl 后如何重新加载 UIViewController

问题描述

我有一个带有 UISegmentedControl 的 viewController 可以在 2 个内容之间切换(按部门或按配方)。当我切换 segmentedControl(它将执行 changeColor 函数)时,我想更改与选择的段相对应的内容。每个段中的内容是几个 UILabel 然后像 view.addSubview(label) 一样添加它们。但是,我当前的代码所做的是将它们相互重叠。

class GroceryViewController: UIViewController {

    var customSC = UISegmentedControl()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupSegmentedControl()
        contentByDepartment()
    }

    func setupSegmentedControl() {
        let items = ["BY DEPARTMENT", "BY RECIPE"]
        customSC = UISegmentedControl(items: items)
        customSC.selectedSegmentIndex = 0

        let frame = UIScreen.main.bounds
        customSC.frame = CGRect(x:frame.minX + 15, y:frame.minY + 100,
                                width:frame.width - 30, height:frame.height * 0.04)
        customSC.layer.cornerRadius = 20
        customSC.backgroundColor = UIColor(hexString: "#F7F7F7")
        customSC.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(hexString: "#FC6A03")], for: UIControl.State.selected)
        customSC.addTarget(self, action: #selector(changeColor), for: .valueChanged)
        view.addSubview(customSC)
    }

    @objc func changeColor(_ sender: UISegmentedControl) {
        switch sender.selectedSegmentIndex {
            case 0:
                contentByDepartment() // contents are several UILabel adding to view as SubView
            case 1:
                contentByRecipe() // contents are several UILabel adding to view as SubView
            default:
                break
        }
    }
}

我已经搜索了解决方案,主要是“UITableView 中的 reloadData()”,我没有。所以我不知道如何在不打扰 UIView 或 UITableView 的情况下重新加载这个 viewController。

问题是:是否可以从某个功能单独重新加载 viewController,以便在显示所选内容之前清除以前的内容以及如何这样做?

编辑: contentByDepartment 和 contentByRecipe 的代码

func contentByDepartment() {
        let recipeHeader = UILabel(frame: CGRect(x:50, y:180, width: 200, height: 20))
        recipeHeader.setActivityDescription(label: "Coffee and Tea")
        recipeHeader.textColor = UIColor(hexString: "#FC6A03")
        recipeHeader.font = UIFont.systemFont(ofSize: 20)

        let ingredient1Name = UILabel(frame: CGRect(x:70,y:210,width:200,height:20))
        ingredient1Name.setActivityDescription(label: "Green tea leaves")
        ingredient1Name.font = UIFont.systemFont(ofSize: 16)

        let ingredient1Amount = UILabel(frame: CGRect(x:72,y:230,width:200,height:20))
        ingredient1Amount.setActivityDescription(label: "1 tablespoons")
        ingredient1Amount.font = UIFont.systemFont(ofSize: 12)

        let ingredient2Name = UILabel(frame: CGRect(x:70,y:260,width:200,height:20))
        ingredient2Name.setActivityDescription(label: "Coffee powder")
        ingredient2Name.font = UIFont.systemFont(ofSize: 16)

        let ingredient2Amount = UILabel(frame: CGRect(x:72,y:280,width:200,height:20))
        ingredient2Amount.setActivityDescription(label: "3 tablespoons")
        ingredient2Amount.font = UIFont.systemFont(ofSize: 12)

        view.addSubview(recipeHeader)
        view.addSubview(ingredient1Name)
        view.addSubview(ingredient1Amount)
        view.layer.addSublayer(drawLine(x1:70, y1:255, x2:350, y2:255, lineWidth:0.3))
        view.addSubview(ingredient2Name)
        view.addSubview(ingredient2Amount)
        view.layer.addSublayer(drawLine(x1:0, y1:310, x2:420, y2:310, lineWidth:0.5))
}

func contentByRecipe() {
        let recipeHeader = UILabel(frame: CGRect(x:50, y:180, width: 200, height: 20))
        recipeHeader.setActivityDescription(label: "Cha-yen")
        recipeHeader.textColor = UIColor(hexString: "#FC6A03")
        recipeHeader.font = UIFont.systemFont(ofSize: 20)

        let ingredient1Name = UILabel(frame: CGRect(x:70,y:210,width:200,height:20))
        ingredient1Name.setActivityDescription(label: "Vanilla extract")
        ingredient1Name.font = UIFont.systemFont(ofSize: 16)

        let ingredient1Amount = UILabel(frame: CGRect(x:72,y:230,width:200,height:20))
        ingredient1Amount.setActivityDescription(label: "1 tablespoons")
        ingredient1Amount.font = UIFont.systemFont(ofSize: 12)

        let ingredient2Name = UILabel(frame: CGRect(x:70,y:260,width:200,height:20))
        ingredient2Name.setActivityDescription(label: "Sugar")
        ingredient2Name.font = UIFont.systemFont(ofSize: 16)

        let ingredient2Amount = UILabel(frame: CGRect(x:72,y:280,width:200,height:20))
        ingredient2Amount.setActivityDescription(label: "3 tablespoons")
        ingredient2Amount.font = UIFont.systemFont(ofSize: 12)

        view.addSubview(recipeHeader)
        view.addSubview(ingredient1Name)
        view.addSubview(ingredient1Amount)
        view.layer.addSublayer(drawLine(x1:70, y1:255, x2:350, y2:255, lineWidth:0.3))
        view.addSubview(ingredient2Name)
        view.addSubview(ingredient2Amount)
        view.layer.addSublayer(drawLine(x1:0, y1:310, x2:420, y2:310, lineWidth:0.5))
}

标签: swiftuiviewcontrolleruisegmentedcontrol

解决方案


您应该学习将相关视图组合在一起。现在,您将所有视图添加为view. 这是非常杂乱无章的,并导致您进入当前情况 - 无法有效地显示/隐藏某些视图。

组织视图的一种方法是将“按部门”视图添加到一个大视图UIView中,并将所有“按配方”视图添加到另一个大视图UIView中。

var byDepartmentContainerView: UIView!
func setUpContentByDepartment() {
    let recipeHeader = UILabel(frame: ...)
    recipeHeader.setActivityDescription(label: "Coffee and Tea")
    recipeHeader.textColor = UIColor(hexString: "#FC6A03")
    recipeHeader.font = UIFont.systemFont(ofSize: 20)

    let ingredient1Name = UILabel(frame: ...)
    ingredient1Name.setActivityDescription(label: "Green tea leaves")
    ingredient1Name.font = UIFont.systemFont(ofSize: 16)

    let ingredient1Amount = UILabel(frame: ...)
    ingredient1Amount.setActivityDescription(label: "1 tablespoons")
    ingredient1Amount.font = UIFont.systemFont(ofSize: 12)

    let ingredient2Name = UILabel(frame: ...)
    ingredient2Name.setActivityDescription(label: "Coffee powder")
    ingredient2Name.font = UIFont.systemFont(ofSize: 16)

    let ingredient2Amount = UILabel(frame: ...)
    ingredient2Amount.setActivityDescription(label: "3 tablespoons")
    ingredient2Amount.font = UIFont.systemFont(ofSize: 12)

    byDepartmentContainerView = UIView(frame: ...)

    byDepartmentContainerView.addSubview(recipeHeader)
    byDepartmentContainerView.addSubview(ingredient1Name)
    byDepartmentContainerView.addSubview(ingredient1Amount)
    byDepartmentContainerView.addSubview(ingredient2Name)
    byDepartmentContainerView.addSubview(ingredient2Amount)
}

var byRecipeContainerView: UIView!
func setUpContentByRecipe() {
    let recipeHeader = UILabel(frame: CGRect(x:50, y:180, width: 200, height: 20))
    recipeHeader.setActivityDescription(label: "Cha-yen")
    recipeHeader.textColor = UIColor(hexString: "#FC6A03")
    recipeHeader.font = UIFont.systemFont(ofSize: 20)

    let ingredient1Name = UILabel(frame: CGRect(x:70,y:210,width:200,height:20))
    ingredient1Name.setActivityDescription(label: "Vanilla extract")
    ingredient1Name.font = UIFont.systemFont(ofSize: 16)

    let ingredient1Amount = UILabel(frame: CGRect(x:72,y:230,width:200,height:20))
    ingredient1Amount.setActivityDescription(label: "1 tablespoons")
    ingredient1Amount.font = UIFont.systemFont(ofSize: 12)

    let ingredient2Name = UILabel(frame: CGRect(x:70,y:260,width:200,height:20))
    ingredient2Name.setActivityDescription(label: "Sugar")
    ingredient2Name.font = UIFont.systemFont(ofSize: 16)

    let ingredient2Amount = UILabel(frame: CGRect(x:72,y:280,width:200,height:20))
    ingredient2Amount.setActivityDescription(label: "3 tablespoons")
    ingredient2Amount.font = UIFont.systemFont(ofSize: 12)

    byRecipeContainerView = UIView(frame: ...)
    byRecipeContainerView.addSubview(recipeHeader)
    byRecipeContainerView.addSubview(ingredient1Name)
    byRecipeContainerView.addSubview(ingredient1Amount)
    byRecipeContainerView.addSubview(ingredient2Name)
    byRecipeContainerView.addSubview(ingredient2Amount)
}

请注意,我省略了视图的框架。请给容器视图一个足够大的框架以包含其所有子视图,并调整子视图的框架,使它们相对于容器视图的坐标系

此外,您似乎在两种方法中都绘制了相同的线条,因此可以一次完成,再也不会。

func drawLines() {
    view.layer.addSublayer(drawLine(x1:0, y1:310, x2:420, y2:310, lineWidth:0.5))
    view.layer.addSublayer(drawLine(x1:70, y1:255, x2:350, y2:255, lineWidth:0.3))
}

viewDidLoad中,您可以调用所有三种方法:

setUpContentByDepartment()
setUpContentByRecipe()
drawLines()
view.addSubview(byDepartmentContainerView)
view.addSubview(byRecipeContainerView)
byRecipeContainerView.isHidden = true

现在您可以执行以下操作:

switch sender.selectedSegmentIndex {
    case 0:
        byRecipeContainerView.isHidden = true
        byDepartmentContainerView.isHidden = false
    case 1:
        byRecipeContainerView.isHidden = false
        byDepartmentContainerView.isHidden = true
    default:
        break
}

PS硬编码视图的帧是一个非常糟糕的主意。它不能本地化,不能适应不同的设备等等。请了解Autolayout Constraints


推荐阅读