本文介绍了快速地以编程方式删除UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在项目中以编程方式添加了多个按钮.我在viewDidLoad方法中使用NSTimer调用该函数,每隔5秒添加更多按钮.我的问题是我想从以前创建的视图中清除按钮,因为在旧按钮之上创建了新按钮.
I have added multiple buttons programmatically in my project. I use NSTimer in my viewDidLoad method to call the function to add more buttons every 5 seconds. My problem is I want to clear the buttons from the view which was previously created as new buttons are getting created on top of the old ones.
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("subtractTime"), userInfo: nil, repeats: true)
}
func subtractTime() {
let button = UIButton(type: UIButtonType.RoundedRect) as UIButton
//button.removeFromSuperview
for var i = 0; i < size; i++ {
for var j = 0; j < size; j++ {
button.frame = CGRectMake(self.x, self.y, BoxWidthHeight, BoxWidthHeight)
button.setTitle("", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.tag = tagcounter
self.view.addSubview(button)
}
}
}
我已经阅读到button.removeFromSuperview
应该可以完成工作,但是我没有将所有按钮都删除,并且屏幕上仍然保留着相同的内容.
I have read that button.removeFromSuperview
should do the work, but i dont get all the buttons removed by it and same remains on the screen.
推荐答案
这应该有所帮助:
for locView in self.view.subviews {
if locView.isKindOfClass(UIButton) {
locView.removeFromSuperview()
}
}
这篇关于快速地以编程方式删除UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!