how to properly show / hide UIView on button click in iOS

16,368

Sometime you (or xCode) can make mistake during connecting IBOutlets/IBAction so if it doesn't work you can remove connection and it back again, mostly it should help.

Also Apple recommendation is use weak instead of strong/retain for IBOutlet property so if you haven't got a good reason you should do:

@property (nonatomic, weak) IBOutlet UIView *subView;
Share:
16,368

Related videos on Youtube

M Zubair Shamshad
Author by

M Zubair Shamshad

SOreadytohelp I 'm iOS developer by profession. I learn programming, Do programming, Dream programming and Love programming. When i'm not writing code, I enjoy with Family, friends and do activities of my interest like playing Chess, Darts and Swimming. :)

Updated on June 04, 2022

Comments

  • M Zubair Shamshad
    M Zubair Shamshad almost 2 years

    I have a UIView, added in my xib file. Through connection inspector join it properly.

    In viewDidLoad: method I am hiding it and on some button pressed showing it.

    Here is my code so far:

    .h file

    @property (nonatomic, retain) IBOutlet UIView   *subView;
    

    in viewDidLoad: method:

    _subView.hidden = YES;
    

    It is hiding this view properly, but on button pressed it is not showing up.

    - (IBAction)customerInvoice:(id)sender
    {
    //self->_subView.hidden = NO;
    _subView.hidden = NO;
    }
    

    Using both ways it is not showing up. Please, help me to resolve it.

    • Greg
      Greg about 10 years
      Are you sure customerInvoice: action fired when you press the button? Add NSLog to make sure it's fired.
    • Stuart
      Stuart about 10 years
      You could also put a breakpoint into the event handler just to check that the code is being hit. This will also allow you to check the value of the _subView in case for some reason it is or has become nil.
    • M Zubair Shamshad
      M Zubair Shamshad about 10 years
      @Greg yes i checked this
    • M Zubair Shamshad
      M Zubair Shamshad about 10 years
      @Stuart what you are saying, it should be nil or not?
    • M Zubair Shamshad
      M Zubair Shamshad about 10 years
      _subView UIView * 0x08c30630 …. set break point and it gives this value
    • Himanshu Joshi
      Himanshu Joshi about 10 years
      Try to log [_subView isDescendantOfView:[self view]]
    • Flexicoder
      Flexicoder about 10 years
      Try using Reveal to see what is happening with your views, revealapp.com it might help identify the issue
    • M Zubair Shamshad
      M Zubair Shamshad about 10 years
      @HimanshuJoshi not showing
    • Himanshu Joshi
      Himanshu Joshi about 10 years
      @Zaibi what is not showing? Did you logged it?
    • Greg
      Greg about 10 years
      I tried this code and it works fine for me (with _subView.hidden). Remove all connection, IBOutlets and IBAction and add it back again.
    • M Zubair Shamshad
      M Zubair Shamshad about 10 years
      @HimanshuJoshi you mean NSLog?
    • Himanshu Joshi
      Himanshu Joshi about 10 years
      Yes @Zaibi use NSLog to log the value
    • AntonijoDev
      AntonijoDev about 10 years
      First you don't want to retain IBOutlet, it is not your property its IBs so you can write weak instead of retain, second you don't need _, just @synthesize subView; and use it without _...Third try not hiding the view in viewDidLoad, just to see if you are really doing the hiding or smth. else is wrong
    • M Zubair Shamshad
      M Zubair Shamshad about 10 years
      @Greg your trick works for me.. i just remove the connection and connect it back, now its working perfectly….
    • Greg
      Greg about 10 years
      Would you like me to add it as an answer so you can accept it?
    • M Zubair Shamshad
      M Zubair Shamshad about 10 years
      @Greg yes sure please add it