I have a view controller (firstViewController) with a switch button. The switch button should show / hide a text field in the secondViewController. How can I do it?
-
How is showed the second view controller? – Larme May 24 '15 at 09:52
-
Have you tried anything by now? – sobolevn May 24 '15 at 09:52
-
Yes but can't get this to work...... I can't figure out how to make the delegate between my first and secondViewController – Flev40 May 24 '15 at 19:55
2 Answers
Your app should be structured according to the Model-View-Controller design pattern. You need to do this:
- Add a
BOOL hideText
variable to your model class - Set
model.hideText
to the value of the switch in the switch event handler - Check
model.hideText
in theviewWillAppear
handler of the view that has the text field - If
model.hideText
is set, hide the text field; otherwise, show it

- 1
- 1

- 714,442
- 84
- 1,110
- 1,523
Very Simple it is.You just have to use delegate
design pattern.Go through this link delegation.
In your code you have to do something like this. I suppose your switch is in secondViewCOntroller and you have to make textField hide on SwitchON in FirstViewController
Step 1) Create Protocol in second View controller like this and create property of that delegate.Pass the value of your switch in switchButtonPressed Method and call that delegate method there.
@protocol secondViewControllerDelegate <NSObject>
-(void)switchButtonPressedOn:(BOOL)switchOn;
@end
@interface secondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISwitch *switchOutlet;
@property(weak,nonatomic)id<secondViewControllerDelegate>delegate;
- (IBAction)switchButtonPressed:(id)sender;
@end
In switchButtonPressed,
- (IBAction)switchButtonPressed:(id)sender {
if(self.switchOutlet.on==YES)
[self.delegate switchButtonPressedOn:YES];
}
Step 2) In FirstViewController,
#import "swiftViewController.h"
@interface ViewController : UIViewController<secondViewControllerDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property BOOL mySwitchState;
@end
Step 3) set delegate=self
in prepareForSegue
method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
secondViewController *destViewController = segue.destinationViewController;
destViewController.delegate = self;
}
-(void)switchButtonPressedOn:(BOOL)switchOn{
NSLog(@"Method Called");
if (switchOn==YES) {
self.textField.hidden=YES;
}else{
self.textField.hidden=NO;
}
}
If you wanted to hide textField in nextViewController then pass the switchState to nextViewController in prepareForSegueMethod
,
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
thirdViewController *thirdVC=segue.destinationViewController;
if (self.switchOutlet.on==YES) {
thirdVC.isSomethingEnabled=YES;
}
}
and in nextViewController, in viewDidLoad
, hide your textField.
if (self.isSomethingEnabled==YES) {
self.textField.hidden=YES;
}
Working perfectly. This is very important concept in iOS. Go through the above link.

- 555
- 4
- 18
-
Thanks Saheb :-)I have the Switch in firstViewController and want the text field in secondViewController to hide :-) – Flev40 May 24 '15 at 17:45
-
Its more simpler than previous.I updated my answer. If it is helpful then vote it right, :-) – Saheb Singh May 25 '15 at 01:13