how to properly show / hide UIView on button click in iOS
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;
Related videos on Youtube
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, 2022Comments
-
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 about 10 yearsAre you sure customerInvoice: action fired when you press the button? Add NSLog to make sure it's fired.
-
Stuart about 10 yearsYou 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 about 10 years@Greg yes i checked this
-
M Zubair Shamshad about 10 years@Stuart what you are saying, it should be nil or not?
-
M Zubair Shamshad about 10 years_subView UIView * 0x08c30630 …. set break point and it gives this value
-
Himanshu Joshi about 10 yearsTry to log
[_subView isDescendantOfView:[self view]]
-
Flexicoder about 10 yearsTry using Reveal to see what is happening with your views, revealapp.com it might help identify the issue
-
M Zubair Shamshad about 10 years@HimanshuJoshi not showing
-
Himanshu Joshi about 10 years@Zaibi what is not showing? Did you logged it?
-
Greg about 10 yearsI 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 about 10 years@HimanshuJoshi you mean NSLog?
-
Himanshu Joshi about 10 yearsYes @Zaibi use
NSLog
to log the value -
AntonijoDev about 10 yearsFirst 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 about 10 years@Greg your trick works for me.. i just remove the connection and connect it back, now its working perfectly….
-
Greg about 10 yearsWould you like me to add it as an answer so you can accept it?
-
M Zubair Shamshad about 10 years@Greg yes sure please add it
-