Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to pushViewController for subview

I have a UINavigationController and I have seperate UIViews that I switch between using a UISegmentControl. On switching the views, I add the view as a subview to my navigation controller's view:

[self.view addSubview:segmentTab1.view];

and

[self.view addSubview:segmentTab2.view];

Then, in the subViews, each has a UITableView, but my issue is, that I am unable to push a new viewController into view in the didSelectRowAtIndexPath method.

The method is called correctly and by setting breakpoints, I can see the method for pushing the view gets called as well, but nothing happens. This is my code for pushing it:

[self.navigationController pushViewController:detailsViewController animated:YES];

I also tried

[super.navigationController pushViewController:detailsViewController animated:YES];

What am I doing wrong - or is is just not possible to do it with a subview?

like image 220
runmad Avatar asked Jan 24 '23 00:01

runmad


2 Answers

When you call -pushViewController, which view controller is self? If you are calling that from within one of your tab subviews, self likely doesn't have a reference to the navigation controller from the top level view that you added it to. You can verify this by getting the memory address of the navigation controller from within the top level view and comparing it to what you have in the subview. Obviously if it's nil or doesn't match, then that's your problem.

You only get a navigation controller "for free" when it's been added to the navigation stack itself with -pushViewController which your subviews haven't been added that way.

like image 196
Matt Long Avatar answered Feb 11 '23 09:02

Matt Long


I had a similar issue when implementing a common header for all the views After many tries , i have fixed it by this -

In all the viewController

- (void)viewWillAppear:(BOOL)animated {

    [self.view addSubview:[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] view]];
    [[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] viewWillAppear:YES];

}

I have referred following post to implement the header view Common XIB in multiple View Controller in iPhone

[self.view addSubview:[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] view]];     // line will load the header subview 

[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] viewWillAppear:YES]; // this is to call the viewWillAppear method of HeaderController where we can write code to check the user is logged or not and change the login button to logout button etc ..

like image 39
Rijesh Avatar answered Feb 11 '23 10:02

Rijesh