views:

284

answers:

1

Hello everyone,

Here is the thing. I currently have a tabBar controller, with several navigation controllers in it. Just like this :

http://tof.canardpc.com/view/d27d1361-c81b-43a0-9b5b-abb13d78e422.jpg

In my first navigation controller, i have a view controller called NewsViewsController. Here is its nib (see picture).

My goal is to show/hide the subviews (with tableview inside) according to the position of the segmented control. This way, I can have two separates viewControllers for each tableview. This is actually working. And here is the result (see picture).

My problem is the following one. When i try to click on a cell, the pushview doesn't work. Indeed my self.navigationController is null. How can i push my detail view using the parent navigation controller ?

Is the architecture of my application wrong ? Thank you =)

A: 

The navigation controller is setup to push and pop the view controller that manages the entire view. Really nothing in the API easily supports multiple view controllers whose views display within the same superview.

Instead of trying to push the next view on one of the tableview subviews, you should manage the nav from the "News View" controller. Even better, you should have just the "News View" controller and have separate classes that implement each table's delegate and datasources (they don't have to be in the view controller its just usually convenient.)

Something like this:

@interface NewsViewController {
    CustomClass1 *newsTableDelegateAndDataSource;
    CustomClass2 *categoryTableDelegateAndDataSource;
}

@interface CustomClass1 {
    UINavigationController *nav;
}

in NewsViewController.m

- (void) viewWillAppear{
    self.newsTableDelegateAndDataSource.nav= // reference to the navigation controller;
    self.categoryTableDelegateAndDataSource.nav = // reference to the navigation controller;
}

Then in the didSelectRow methods of one of CustomClasses:

nextViewController=// intialize or get reference to next view controller
[self.nav pushViewController:nextViewController animated:YES];

The nav will push the nextViewController on top of NewsViewController and display the new screen. When it is done, it pops itself and NewsViewController reappears in the state you left it in.

This way you only have one view controller active at anyone time so the API works smoothly but you encapsulate the behavior of each table in its own class.

Everybody is happy.

TechZen
slaapwagen