Search code examples
objective-ciosuisplitviewcontrolleriboutlet

I lose access to my IBOutlets when making view controller new detail view controller


I have an iPad master-detail where the main detail controller is a navigation controller and depending on the table row (in the master view controller) selected, I may or may not replace the view controller managed by the detail navigation controller.

This seems to work fine, except that I lose access to the new detail view controller's IBOutlets when this move is made.

Here's me switching the navigation controller's view controller:

CustomerDetailViewController *subview = [[CustomerDetailViewController alloc] initWithNibName:@"CustomerDetailViewController" bundle:nil];            
[subview setTitle:@"Testing"];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.splitViewController.delegate = subview;
[app.detailNavigationController setViewControllers:[NSArray arrayWithObjects:subview, nil]];
[subview setData:[custData objectForKey:@"name"]];

custData is an NSDictionary containing my view information. Here is the setData command:

- (void)setData:(NSDictionary *)cust {

    NSLog(@"%@\n", [cust valueForKey:@"name"]);
    self.nameLabel.text = [cust valueForKey:@"name"];
    NSLog(@"%@\n", self.nameLabel.text);
}

So what happens is, subview becomes the new view controller but the label does not get changed - however, those two log commands are executed. The label is synthesized and wired up using IB and works if I push subview as a new view controller instead of replace it.


Solution

  • I'd say, the view is not yet initialized. Outlets are first connected in the viewDidLoad method. Try putting a log statement in the viewDidLoad to find out, which one gets called first.

    If the viewDidLoad is called after your setData method, you can only set a local variable of the CustomerDetailViewController which is then read by viewDidLoad which sets the label accordingly.