更改 UISegmentedControl 后如何重新加载 UIViewController

How to reload UIViewController after changing UISegmentedControl

我有一个带有 UISegmentedControl 的 viewController 可以在 2 个内容之间切换(按部门或按食谱)。当我切换 segmentedControl(它将执行 changeColor 函数)时,我想更改与所选段对应的内容。每个segment中的内容都是几个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
        }
    }
}

我已经搜索了解决方案,大多数是 "reloadData() in UITableView",我没有。所以我不知道如何在不影响 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))
}

您应该学会将相关视图组合在一起。现在,您正在将所有视图添加为 view 的子视图。这是非常没有条理的,并导致您目前的情况 - 无法有效地 show/hide 某些视图。

组织视图的一种方法是将 "by department" 个视图添加到一个大 UIView 中,并将所有 "by recipe" 个视图添加到另一个大 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
}

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