Chizx
Chizx

Reputation: 349

Grabbing data from a container view

In a UIViewController A I have a container-view-embedded UITableViewController B. In other words, ViewController B is in a container view in ViewController A. A simply contains a Navigation bar and a "next" UIButton at the bottom of the view. I need the data from the UITextField's in B to be sent through a prepareForSegue: method that is in A.m, how should the NSMutableArray that contains the data from the UITextfield's in B be shared with A?

Upvotes: 1

Views: 51

Answers (1)

rdelmar
rdelmar

Reputation: 104082

The data can't be sent throughout the segue, because the segue is executed as soon as the controllers are instantiated, before any user interaction with the text fields. There are several ways you could do this, but the easiest is to have an array in A that you set equal to the array in B by using self.parentViewController to access it. The following code is in B (be sure to import A.h into the B.m file,

ControllerA *aController = self.parentViewController;
aController.arrayToGet = self.arrayToPass;

You can, if you prefer, create a property in B that points to A (@property (strong,nonatomic) ControllerA *pointerToA;), and set its value in prepareForSegue. You can then use that instead of self.parentViewController to access any of A's properies or methods

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    ControllerB *bVC = segue.destinationViewController;
    bVC.pointerToA = self;
}

Upvotes: 1

Related Questions