代码之家  ›  专栏  ›  技术社区  ›  user2872856

删除ViewDidAppear中的上一个子视图

  •  -1
  • user2872856  · 技术社区  · 7 年前

    我在使用以下方法 viewDidAppear 添加 UILabel 显示今天日期的子视图。

        let date = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "EEEE, dd MMM yyyy"
        let todayDate = formatter.string(from: date)
    
        let todayLabel = UILabel(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
        todayLabel.font = UIFont.systemFont(ofSize: 16)
        todayLabel.text = "Today is " + todayDate
        self.view.addSubview(todayLabel)
    

    但是,当系统日期更改时, uilabel公司 在现有的基础上添加:

    enter image description here

    我试图删除子视图并再次添加新的子视图,但问题没有解决。

        let date = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "EEEE, dd MMM yyyy"
        let todayDate = formatter.string(from: date)
    
        let todayLabel = UILabel(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
        todayLabel.font = UIFont.systemFont(ofSize: 16)
        self.view.addSubview(todayLabel)
        todayLabel.removeFromSuperview()
        self.view.addSubview(todayLabel)
        todayLabel.text = "Today is " + todayDate
    

    如何解决这个问题?

    2 回复  |  直到 7 年前
        1
  •  1
  •   rob mayoff    7 年前

    您尝试删除旧标签并添加新标签:

    let todayLabel = UILabel(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
    todayLabel.font = UIFont.systemFont(ofSize: 16)
    self.view.addSubview(todayLabel)
    todayLabel.removeFromSuperview()
    self.view.addSubview(todayLabel)
    todayLabel.text = "Today is " + todayDate
    

    但你的代码是这样的:

    1. 创建一个新标签,由局部变量引用 todayLabel 是的。
    2. 将新标签添加到根视图。
    3. 从根视图中删除新标签。
    4. 将新标签添加到根视图。

    在任何时候,您都不会对以前调用中创建的任何标签执行任何操作 viewDidAppear 是的。

    // Cached reference to the label, if it exists
    private var _todayLabel: UILabel?
    
    // Creates the label on demand.
    private var todayLabel: UILabel {
        if let label = _todayLabel { return label }
        let label = UILabel(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
        _todayLabel = label
        label.font = UIFont.systemFont(ofSize: 16)
        self.view.addSubview(label)
    }
    
    private var todayText: String {
        let date = Date()
        let formatter = DateFormatter()
        formatter.dateFormat = "EEEE, dd MMM yyyy"
        return formatter.string(from: date)
    }
    
    override func viewDidAppear() {
        super.viewDidAppear()
        todayLabel.text = "Today is " + todayText
    }
    
        2
  •  0
  •   Mahendra    7 年前

    解决方案:

    无论何时添加新标签,都可以将其创建为特性或将标记指定给标签。

    • 使用属性,如 rob mayoff

    • 为标签分配标记

      let todayLabel = UILabel(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
      
      todayLabel.tag = 1001 // or any other number you want to assign
      
      todayLabel.font = UIFont.systemFont(ofSize: 16)
      todayLabel.text = "Today is " + todayDate
      self.view.addSubview(todayLabel)
      
    • 在添加新标签之前,可以按标签号将其删除。

      self.view.viewWithTag(1001).removeFromSuperview()
      // code here...to add a new label