Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 7    Views: 77

PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
04/18/14 07:29 PM (10 years ago)

Method within a method?

Hi all, Wondering if this is even possible: A UISwitch in the navbar (I know that is possible) that 'pops up' another UIwidget (in this case a UITextField) on the same screen, then makes it disappear again when the UISwitch is toggled off again. I can get a button to go to another screen, but I haven't yet been able to have a button do something on its 'own' screen. I imagine that the UISwitch would call a method that would already be sitting within the viewDidLoad method of BT_viewController, but the 'pop-up' method would need to be initially "hidden", then magically appear and disappear depending on what the UISwitch is doing. So it seems that I'm looking to somehow call a method within the viewDidLoad method but have it controlled by a customised button in the navbar. Eek. If anyone has any experience with this I would really appreciate any guidance (or even "no way you're crazy" if I'm entering la la land...) Cheers Paddy
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/18/14 07:42 PM (10 years ago)
Easy. Create the textfiled in the ViewDidLoad method and set it to hidden. In the method for the switch, set the hidden property to true or false.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/19/14 06:12 PM (10 years ago)
Thanks Chris, I'm very encouraged! However the method to connect the switch has me beat. Here's what I've tried: 1. In my ViewDidLoad method I create the switch like this: UISwitch *citySearchSwitch = [[UISwitch alloc] initWithFrame:(CGRectZero)]; [citySearchSwitch addTarget: self action: @selector(magicSwitch:) forControlEvents:UIControlEventValueChanged]; UIBarButtonItem *citySearch = [[UIBarButtonItem alloc] initWithCustomView:(citySearchSwitch)]; //Load the switch button... NSArray *actionButtonItems = @[citySearch]; self.navigationItem.LeftBarButtonItems = actionButtonItems; (the array holds two buttons, I'm just de-cluttering for this post) Cool, I have an animated switch (that does nothing yet). 2. Then I create the text field widget that I want to be controlled by the switch and set it to be hidden by default: UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(16, 9, 250, 30)]; //other properties etc etc etc ending with... tf.hidden = YES; [self.view addSubview:tf]; And that works because I can show/hide it by changing tf.hidden=YES to tf.hidden=NO and it works fine. 3. ...and now I create a method to change the hidden status of my UITextField, using the magicSwitch selector I specified in Step 1 above. Something like this: - (void) magicSwitch { tf.hidden=FALSE; } And this is where I come unstuck, Xcode tells me that tf is an undeclared identifier - but I thought that I had defined it earlier. There's clearly a connecting step that I'm missing. What am I not doing? Any pointers gratefully accepted! Thanks, Paddy.
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/19/14 06:25 PM (10 years ago)
You need to declare the textfield as a property in the .h file and synthesize it.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/19/14 07:04 PM (10 years ago)
Of course, thank you. [Bangs head on table] That got rid of the error. And I think my magicSwitch method is 'more correct' now too: -(void) magicSwitch { [tf setHidden:FALSE]; } But the switch still isn't connecting with the UITextField. my .h file now has @property (nonatomic, strong) IBOutlet UITextField *tf; and tf is synthesized in .m I know it's something small but it's escaping me...
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
04/19/14 07:25 PM (10 years ago)
verify that your magicSwitch method is being called. Add in a debug statement like so: [BT_debugger showIt:self theMessage:@"magicSwitch"]; If it is being called, then "tf" still isn't connected up right. Make sure when you declare the textField, you're not overwriting the property. For instance, if you still have this: UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(16, 9, 250, 30)]; then you'll be overwriting the property. Instead, do this; tf = [[UITextField alloc] initWithFrame:CGRectMake(16, 9, 250, 30)]; (notice the slight difference - in the second one, we dropped the UITextField* reference. That's because it's already defined when you setup the property in the .h file.
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/19/14 07:38 PM (10 years ago)
Thanks Chris... must head off to Easter celebrations but will get back on this tonight. Thank you!! Cheers Paddy
 
PaddyO
Lost but trying
Profile
Posts: 189
Reg: Sep 11, 2013
Geelong
5,190
like
04/20/14 05:57 PM (10 years ago)
With a fair bit more fiddling, I have it working! Thanks Chris, much appreciated. I'll post the final code in a separate post that will have a more informative topic title. EDIT: Here - https://www.buzztouch.com/forum/thread.php?fid=74A8A995DC1F0A22B2D0D03&tid=74A8A995DC1F0A22B2D0D03 Cheers Paddy.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.