Shinonuma
Shinonuma

Reputation: 491

Getting a property from parent class

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?

Upvotes: 0

Views: 149

Answers (3)

deathhorse
deathhorse

Reputation: 647

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

Upvotes: 0

Mundi
Mundi

Reputation: 80265

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!

Upvotes: 1

SashaZd
SashaZd

Reputation: 3319

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

Upvotes: 0

Related Questions