jmcastel jmcastel - 2 months ago 24
iOS Question

Swift : Hide button in subview

I am trying to hide a UIButton in a subView when a function is fired.
I have multiple views with that hierarchy :

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)


I want to hide the takePhotoButton when the following func is fired, how could I do that ?

func takePhoto(takePhotoButton: UIButton!) {
takePhotoButton.hidden = true
}

Answer

You have to make the UIButton a property of the class if you want to keep a reference to it. Then you can access it using self.takePhotoButton.