1

I found a lot of how to use methods of chlidViewController. But I couldn't find how to change and set value of uitextfield and uiswitch from childViewController.

ChildViewController.h:

@protocol VVInformationTableViewControllerDelegate;

@interface VVInformationTableViewController : UITableViewController

@property (weak, nonatomic) id<VVInformationTableViewControllerDelegate> delegate;

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@property (weak, nonatomic) IBOutlet UITextField *surnameTextField;

@property (weak, nonatomic) IBOutlet UITextField *emailTextField;

@property (weak, nonatomic) IBOutlet UITextField *locationTextField;

@property (weak, nonatomic) IBOutlet UITextField *headlineTextField;

@property (weak, nonatomic) IBOutlet UITextField *positionTextField;

@property (weak, nonatomic) IBOutlet UITextField *companyTextField;

@property (weak, nonatomic) IBOutlet UISwitch *messagesEnable;

@end

ParentViewControler.m:

   - (void)viewDidLoad
{
    self.currentAttendee = [VVAPIClient sharedClient].currentUser;

    NSParameterAssert(self.currentAttendee);

    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:YES];

    self.infoTableController = [[VVInformationTableViewController alloc] initWithNibName:@"InformationTableViewController" bundle:nil];
    [self addChildViewController:self.infoTableController];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.infoTableController.nameTextField.text = self.currentAttendee.firstName?:@"";
    self.infoTableController.surnameTextField.text = self.currentAttendee.lastName?:@"";
    self.infoTableController.emailTextField.text = self.currentAttendee.email?:@"";
    self.infoTableController.locationTextField.text = self.currentAttendee.location?:@"";

    self.infoTableController.headlineTextField.text = self.currentAttendee.headline?:@"";
    self.infoTableController.positionTextField.text = self.currentAttendee.position?:@"";
    self.infoTableController.companyTextField.text = self.currentAttendee.company?:@"";

}

-(void)viewDidLayoutSubviews{

    self.infoTableController.messagesEnable.on = NO;
    self.infoTableController.nameTextField.tag = 0;
    self.infoTableController.surnameTextField.tag = 1;
    self.infoTableController.emailTextField.tag = 2;
    self.infoTableController.locationTextField.tag = 3;

    self.infoTableController.headlineTextField.tag = 5;
    self.infoTableController.positionTextField.tag = 6;
    self.infoTableController.companyTextField.tag = 7;
}

Thanks for help.

xav9211
  • 191
  • 1
  • 3
  • 14
  • Generally speaking, having one view controller changing properties on another one should be reconsidered. It violates the desired separation of functionality between objects. In this case, you'd be better off passing `currentAttendee` into the `VVInformationTableViewController` as a property, and letting the child do it's own thing. – David Berry May 22 '14 at 23:27

1 Answers1

1

As david says in his comment, don't.

It violates the encapsulation of the other view controller, and leads to spaghetti code.

You should treat another VCs (View Controller's) views as private.

What you should do is add properties to the child view controller to hold strings and other state data that you need to display. Then in your child view controller's viewWillAppear method, you can take the settings and apply them to your view hierarchy.

In your case, since what you're doing is displaying a whole bunch of information about "currentAttendee", (which I guess is a model object) you might want to think about passing a pointer to the whole attendee object to the child, and letting it display the information itself.

Or, of the child can edit the object, you might want to pass a copy, and use a delegate method when you want to commit the changes made in the child, or simply return if you want to discard changes.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I can have informations about currentAttendee in both controllers, but in parentViewController I need to know when and wich textField I use because I need to scroll my scrollView in parentController when keyboard appear. – xav9211 May 23 '14 at 05:21
  • That's fine. The parent view controller manages its views, and the child view controller manages ITS views. The parent view controller shouldn't need to know which view is selected in the child view controller however. – Duncan C May 23 '14 at 10:13
  • Ok, I don't need to have delegate of uiTextFields in parrentViewController but I need to get all gesture recognizers which dismiss my keyboard and I need method scrollViewWillBeginDragging from childViewController was called in parrentViewController. How can I do that? – xav9211 Jun 02 '14 at 09:56