【发布时间】:2015-09-01 19:14:11
【问题描述】:
在我的视图控制器的 viewDidLoad 中,我添加了一个按钮:
let tutorialButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
tutorialButton.frame = CGRectMake(0, (UIScreen.mainScreen().bounds.size.height/4)*3, UIScreen.mainScreen().bounds.size.width, 20)
tutorialButton.titleLabel?.textAlignment = NSTextAlignment.Center
tutorialButton.backgroundColor = UIColor.clearColor()
tutorialButton.setTitle("View Quick Tutorial", forState: UIControlState.Normal)
tutorialButton.addTarget(self, action: "giveTutorial:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(tutorialButton)
按钮出现在我想要的位置,并且该部分效果很好。但是在它达到目的并且我不再希望它可见之后,如何从视图中删除该按钮?我研究过它通常是这样完成的:
buttonName.removeFromSuperview
但是,当我输入 buttonName 时,它无法识别该按钮。我猜是因为没有 IBOutlet。那么我可以在一个方法中添加什么代码来删除这个按钮呢?
【问题讨论】:
-
将按钮放入实例变量中,然后调用
instanceVariable.removeFromSuperview() -
使
tutorialButton成为控制器类的成员。然后你可以使用self.tutorialButon.removeFromSuperview。如果您希望能够删除它,则需要保留对它的引用以便将其删除。
标签: ios swift uibutton iboutlet