0

I've added a view controller as child like this:

UIViewController *sendViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"send"];
[self addChildViewController:sendViewController];

In the sendViewController I got this property:

@property (strong, nonatomic) IBOutlet StringInputTableViewCell *nameCell;

and in that class I can do something like self.nameCell.textField.text = @"test"; to set the textField to test

But how can I get this text thats filled in the textField in the parent class?

Shinonuma
  • 491
  • 2
  • 8
  • 19

3 Answers3

1

You are mixing model and view, thus breaking the MVC design pattern.

You should not try to read what is the content of a UI element. Rather you should have all the data (i.e. model) and the view (i.e. the UI, such as text fields) managed by a controller.

There are (easy) ways to get to this information, but I strongly advise you not to go down that road!

Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Well I guess the controller that manages those is the sendViewController, but I added this controller as a child on another controller and in that controller I need to get the TextField information – Shinonuma Apr 04 '13 at 14:14
  • No, you don't. You need a good model structure that tells you what the text in this text field should be. – Mundi Apr 04 '13 at 14:22
0

Basic inheritance between the parent and child class should allow you to pass the property forward.

You'll need to create a child object of the class say obj. Then to get the text value of the field you'll use (in the parent class)

id obj = [[ChildClassName alloc] init];
NSString *myChildsText = obj.nameCell.textField.text;  // will get the value @"test" as assigned in the childclass. 

Or of course, you can create a getter and setter in the Child Class for your @property. For example ::

- (IBOutlet)nameCell {
 // returns the value
}

- (IBOutlet)setNameCell :(StringInputTableViewCell)newValue {
 //set the value to the @synth value here… 
}

then you can call the child objects getters/setters as below ::

NSString *text = [obj nameCell]; //etc etc

SashaZd
  • 3,315
  • 1
  • 26
  • 48
0

You can use 4 approaches in here

a) keep reference to you childviewcontroller -> get the value directly b) use delegate method ( best for observing changes in your textfield ) c) send NSNotification -> this comes with overhead, well at certain points it can help you a lot d) KVO

deathhorse
  • 637
  • 6
  • 15