我试图在触发函数时隐藏子视图中的UIButton。 我有这种层次结构的多个视图:
var takePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
takePhotoButton.addTarget(self, action:"takePhoto", forControlEvents:UIControlEvents.TouchUpInside)
var savePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
let view2:UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
self.view.addSubview(photoMask)
photoMask.addSubview(view2)
view2.addSubview(takePhotoButton)
我想在触发以下func时隐藏takePhotoButton,我该怎么做?
func takePhoto(takePhotoButton: UIButton!) {
takePhotoButton.hidden = true
}
答案 0 :(得分:5)
如果要保留对它的引用,则必须使UIButton成为该类的属性。然后,您可以使用self.takePhotoButton
访问它。
答案 1 :(得分:2)
在添加目标时使用takePhoto:作为选择器,调用方法时将传递按钮。
var takePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
takePhotoButton.addTarget(self, action:"takePhoto:", forControlEvents:UIControlEvents.TouchUpInside)
var savePhotoButton : UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
let view2:UIView = UIView(frame: CGRectMake(0, 0, 320, 568))
self.view.addSubview(photoMask)
photoMask.addSubview(view2)
view2.addSubview(takePhotoButton)
然后,隐藏方法中的按钮,
func takePhoto(takePhotoButton: UIButton!) {
takePhotoButton.hidden = true
}
答案 2 :(得分:2)
,您需要使用isHidden
而不是hidden
subview.isHidden = true
特别是在这种情况下,
self.subview.isHidden = true
答案 3 :(得分:1)
制作按钮的参考变量。由此将其隐藏属性设置为true。
self.yourReferenceVariable.hidden = true