在viewDidAppear中删除上一个子视图

时间:2018-06-23 02:00:34

标签: ios swift

我正在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 个答案:

答案 0 :(得分:1)

这是您尝试删除旧标签并添加新标签的方法:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
  <div class="row">
    <div class='col-6 col-sm-4 col-md-3'>
      <div class="card mb-2">
        <img class='card-img-top' src='./img/$imagen' alt='$categoria'>
        <div class='card-body'>
          <form action='go.php' method='post'>
            <h5 class='card-title'>$categoria</h5>
            <input type='hidden' name='producto' id='hiddenField' value='$categoria'>
            <input class='btn' type='submit' value='ver más'>
          </form>
        </div>
      </div>
    </div>
    <div class='col-6 col-sm-4 col-md-3'>
      <div class="card mb-2">
        <img class='card-img-top' src='./img/$imagen' alt='$categoria'>
        <div class='card-body'>
          <form action='go.php' method='post'>
            <h5 class='card-title'>$categoria</h5>
            <input type='hidden' name='producto' id='hiddenField' value='$categoria'>
            <input class='btn' type='submit' value='ver más'>
          </form>
        </div>
      </div>
    </div>
    <div class='col-6 col-sm-4 col-md-3'>
      <div class="card mb-2">
        <img class='card-img-top' src='./img/$imagen' alt='$categoria'>
        <div class='card-body'>
          <form action='go.php' method='post'>
            <h5 class='card-title'>$categoria</h5>
            <input type='hidden' name='producto' id='hiddenField' value='$categoria'>
            <input class='btn' type='submit' value='ver más'>
          </form>
        </div>
      </div>
    </div>
    <div class='col-6 col-sm-4 col-md-3'>
      <div class="card mb-2">
        <img class='card-img-top' src='./img/$imagen' alt='$categoria'>
        <div class='card-body'>
          <form action='go.php' method='post'>
            <h5 class='card-title'>$categoria</h5>
            <input type='hidden' name='producto' id='hiddenField' value='$categoria'>
            <input class='btn' type='submit' value='ver más'>
          </form>
        </div>
      </div>
    </div>
    <div class='col-6 col-sm-4 col-md-3'>
      <div class="card mb-2">
        <img class='card-img-top' src='./img/$imagen' alt='$categoria'>
        <div class='card-body'>
          <form action='go.php' method='post'>
            <h5 class='card-title'>$categoria</h5>
            <input type='hidden' name='producto' id='hiddenField' value='$categoria'>
            <input class='btn' type='submit' value='ver más'>
          </form>
        </div>
      </div>
    </div>
    <div class='col-6 col-sm-4 col-md-3'>
      <div class="card mb-2">
        <img class='card-img-top' src='./img/$imagen' alt='$categoria'>
        <div class='card-body'>
          <form action='go.php' method='post'>
            <h5 class='card-title'>$categoria</h5>
            <input type='hidden' name='producto' id='hiddenField' value='$categoria'>
            <input class='btn' type='submit' value='ver más'>
          </form>
        </div>
      </div>
    </div>
    <div class='col-6 col-sm-4 col-md-3'>
      <div class="card mb-2">
        <img class='card-img-top' src='./img/$imagen' alt='$categoria'>
        <div class='card-body'>
          <form action='go.php' method='post'>
            <h5 class='card-title'>$categoria</h5>
            <input type='hidden' name='producto' id='hiddenField' value='$categoria'>
            <input class='btn' type='submit' value='ver más'>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

但这是您的代码的作用:

  1. 创建一个新标签,该标签由局部变量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. 将新标签添加到根视图。
  3. 从根视图中删除新标签。
  4. 将新标签添加到根视图。

在任何时候,您都不会对在先前对todayLabel的调用中创建的任何标签进行任何操作。

为什么要创建一个新标签?我建议您在第一次使用时创建一次标签,然后根据需要重新使用它:

viewDidAppear

答案 1 :(得分:0)

实际上,您要删除已创建但未添加为子视图的新标签,然后要删除显然不起作用的标签。

解决方案:

无论何时添加新标签,都可以将其创建为属性或为标签分配标签。

  • 使用a

  • 演示的属性
  • 为标签分配标签

    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)