CodeTry CodeTry -4 years ago 85
iOS Question

Not able to access container view's tableview controller elements

I have a view controller (vc1) which has a container view and below that it has a save button . Container view is embed segued to tableview controller(tvc1) having static cells . Each cell has labels and textfields.
I am trying to get the contents of textfields in tvc1 in vc1 .
I tried two methods :

Preparefor segue in vc1:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"prepare for segue called in settings view controller.");
NSString * segueName = segue.identifier;
if ([segueName isEqualToString: @"embedseguesettings"]) {
NSLog(@"embed segue called ");
settingsTableViewController *tvc1 = (settingsTableViewController *)segue.destinationViewController;
tvc1.surlTextField.text = @"i got u ";
}
}


i am getting print for "embed segue called ", but the textfield in tvc1 is not updated with my text "i got u". Have i got the tvc1 object properly ?

Instantiate tvc1 in vc1:

- (IBAction)save:(id)sender {
settingsTableViewController *tvc1 = [self.storyboard instantiateViewControllerWithIdentifier:@"settingsTableView"];
NSLog(@"surl textfeild = %@",tvc1.surlTextField.text);
}


But i always get null as output. i type some text to surl textfield an press save . i always get null.

settingsTableViewController is the container view's table view controller and i have only set the no of sections and no of rows in that class. Do i need to dequeue cells , i should not be ? , its a static tableview.

Answer Source

The problem is that you're setting the textField value in tvc1 before the view has loaded.

You should add a property in tvc1…

@property (nonatomic, strong) NSString* someText;

then in your prepareForSegue method, assign to someText and also keep a reference to tvc1

if ([segueName isEqualToString: @"embedseguesettings"]) {
    settingsTableViewController *tvc1 = (settingsTableViewController *)segue.destinationViewController;
    tvc1.someText = @"i got u ";   
    self.tvc1 = tvc1     
}

and in your tvc1 viewDidLoad

tvc1.surlTextField.text = self.someText

Then in your save method:

- (IBAction)save:(id)sender {
    // Don't instantiate a new tvc1
    NSLog(@"surl textfeild = %@",self.tvc1.someText);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download