我有一个 UIButton
,它的文本设置为 "Play"
:
@IBOutlet weak var boatTypeGamePlayButton: UIButton!
当这个按钮被点击并且它的文本被设置为"play"
时,我想执行一些功能:
@IBAction func boatTypeMatchingGameButtonTapped(sender: AnyObject) {
if boatTypeGamePlayButton.titleLabel?.text == "Play" {
boatTypeMatchingGameLabel.hidden = true
boatTypeGameLevelContainer.hidden = false
boatTypeMatchingGameTitle.text = "Select Level"
boatTypeGamePlayButton.setTitle("Cancel", forState: UIControlState.Normal)
}
boatTypeGameLevelContainer
是一个 UIView,里面有四个按钮。
问题是 boatTypeGameLevelContainer
确实变得可见,它应该是可见的,但其中的按钮却不可见。我尝试为按钮制作 IBOutlet
并将它们设置为在此函数中可见,但它没有改变任何东西。
我设置的所有其他方法都有效...
我还有一个 else
语句(意思是 boatTypeGamePlayButton
的文本设置为 "Cancel"
):
else {
boatTypeMatchingGameLabel.hidden = false
boatTypeGameLevelContainer.hidden = true
boatTypeMatchingGameTitle.text = "Boat Type Matching Game"
boatTypeGamePlayButton.titleLabel?.text = "Play"
boatTypeGamePlayButton.setTitle("Play", forState: UIControlState.Normal)
}
如何确保按钮出现?感谢您的支持。
更新:我正在使用 Storyboard添加按钮,并且我没有在代码中更改它们的任何属性。
最佳答案
您在两个语句中添加了相同的标题,这导致了混淆。如果两个实例的文本都是 Play,则会产生一些问题,并且会取消。我建议将条款分开。
@IBAction func boatTypeMatchingGameButtonTapped(sender: AnyObject) {
if boatTypeGamePlayButton.titleLabel?.text == "Play" {
//some code here
}
else if boatTypeGamePlayButton.titleLabel?.text == "Cancel"{
//do your other code here
} else {
//do something if none of those statements are true
}
编辑
如果您声明 View 正在填充并且您的背景显示您在 Storyboard中设置的颜色,则可能是由于缺少自动布局约束。通常,当您为特定大小创建 Storyboard或 xib 并在不同大小上运行时,您指定没有为 View 及其组件设置的约束,它们不会像您看到的那样编译和运行在 Storyboard中。
您应该查看 Auto Layout Guide苹果已经提供。它内容广泛,是您 future 项目的重要资源。如果你学习得当,自动布局是非常直观的。祝你好运
关于ios - UIView 没有隐藏,但它里面的按钮是?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27709122/