Swift : Hide button in subview

18,634

Solution 1

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.

Solution 2

Use takePhoto: as the selector while adding target and the button will be passed when the method is called.

 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)

Then, hide button in the method,

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

Solution 3

in Swift 3.0, You need to use isHidden instead of hidden

subview.isHidden = true

particularly in this case,

self.subview.isHidden = true

Solution 4

Make a reference variable of button. Set its hidden property to true by this.

self.yourReferenceVariable.hidden = true
Share:
18,634
jmcastel
Author by

jmcastel

Updated on June 05, 2022

Comments

  • jmcastel
    jmcastel almost 2 years

    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
       }
    
  • jmcastel
    jmcastel over 9 years
    Ok when i add the button as a selector i can access it in the takePhoto method but when i push the button in my app, i have a new error : unrecognized selector sent to instance
  • Sandeep
    Sandeep over 9 years
    Yes you should also add the method as I added above, takePhoto(takePhotoButton: UIButton!)
  • jmcastel
    jmcastel over 9 years
    hi, can't access it if the button isn't a selector. If yes the my app crash with this error : unrecognized selector sent to instance
  • jmcastel
    jmcastel over 9 years
    i did it, look at my edited post witch is now my actual code.
  • Sandeep
    Sandeep over 9 years
    But you didn't set the selector as takePhoto:, takePhotoButton.addTarget(self, action:"takePhoto:", forControlEvents:UIControlEvents.TouchUpInside)
  • John
    John over 2 years
    Curious. When you call action:"takePhoto:" in addTarget, I don't see any variables being passed to the takePhoto target. This works to hide it, but how does the button get passed to the takePhoto func so it's able to manipulate it?
  • Sandeep
    Sandeep over 2 years
    @John This is known as target action pattern in cocoa. The target object which takes in the action is passed along the method's parameter. For reference, developer.apple.com/library/archive/documentation/General/…