IOS Deletes dynamically created UIButton via tag

  • 2021-11-14 07:18:07
  • OfStack

IOS deletes dynamically created UIButton via tag

Dynamic creation

Direct code


    // Start button 
    let btnStart=UIButton()
    btnStart.tag=100 // The point is here 
    btnStart.frame=CGRectMake(CGRectGetMidX(self.frame) / 2 + 25, CGRectGetMidY(self.frame) / 2 + 30, 100, 40)
    btnStart.setTitle(" Begin ",forState: UIControlState.Normal)
    btnStart.backgroundColor=UIColor.blueColor()
    self.view?.addSubview(btnStart)

Delete via tag

The reason for deleting in this way is that the UIButton created when switching Scence does not disappear, and the operation for Scence is not inside 1 method


self.view?.viewWithTag(100)?.removeFromSuperview()

Postscript

Recently, I started to study swift development and iOS development from scratch, and I am not skilled. Please correct me!

Treat the 1-cut around you with a learning attitude


Related articles: