CodeTry
CodeTry

Reputation: 312

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.

Upvotes: 1

Views: 53

Answers (2)

CodeChanger
CodeChanger

Reputation: 8371

As per - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender flow its still not allocated it outlets so...

The UIViewController isn't loaded at that moment. You need to set @property values and access that in the viewDidLoad() method and setup your textFiled text accordingly.

Upvotes: 1

Ashley Mills
Ashley Mills

Reputation: 53181

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);
}

Upvotes: 1

Related Questions