【问题标题】:How to reload UIViewController after changing UISegmentedControl更改 UISegmentedControl 后如何重新加载 UIViewController
【发布时间】:2020-05-19 16:20:39
【问题描述】:

我有一个带有 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))
}

【问题讨论】:

  • 您当前的代码似乎可以执行您想要的操作...您当前的代码是做什么的?
  • 它确实改变了内容,但它们会相互重叠。我希望做的是重新加载它,以便首先清除以前的内容,然后显示所选内容
  • 然后先清除前一个。你必须自己做。没有人会为您做...您可以使用循环遍历要删除的视图的父视图的subviews,然后调用removeFromSuperView。或者,将部门和配方内容视图添加为不同视图的子视图。这样,当分段控件发生变化时,您可以只显示/隐藏相应的超级视图。
  • 如果您显示contentByDepartment 和contentByRecipe,我可能可以发布答案。
  • 我已经编辑了帖子以显示这两个功能。

标签: swift uiviewcontroller uisegmentedcontrol


【解决方案1】:

您应该学会将相关视图组合在一起。现在,您将所有视图添加为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
}

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

【讨论】:

  • 非常感谢。您的解决方案非常清晰和有条理。我从中学到了很多,我会尝试更多地了解您的建议。
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 2011-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多