Obj-C, Navigation Controller with Tab Controller a

2019-08-10 12:27发布

问题:

When I first setup my app I had some issues getting a single navigation controller to work.

I have several screens behind each tab item. I think the problem I was getting was that view controllers would show within the wrong tabs, when switching between them. I'm not bothered about keeping the last view controller used open within each tab, in fact I hide the tab bar to stop this anway now.

So at the moment I have navigation controller files for each of my tabs. I have them assigned in IB, in the mainWindow.

And I use them like this...

CategorySelTableViewController *nextController = 
          [[[CategorySelTableViewController alloc] initWithNibName:
          @"CategorySelTableView" bundle:nil] autorelease];
nextController.hidesBottomBarWhenPushed = YES;

MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] 
         delegate];

[delegate.billsndepsNavController pushViewController:nextController animated:YES];

However, I have some leaks.

I can't release my delegate, it causes an error.

My colleuge suggests that I should just be using self.navigationcontroller.

But this is a big change for me, I'd like to know definetively if I'm doing this wrong before I make the changes ?

回答1:

When a view controller is pushed into the stack, it has two ways to access the navigation controller:

  • Using self.navigationController.
  • Accessing the navigation controller ivar in the delegate:
    [UIApplication sharedApplication].delegate.navigationController

Both are equivalent but the first one is shorter, so it tends to be used more. There is no benefit from switching from one to the other. The only reason to do the extra typing is when you are not in a pushed view controller, eg: a view controller used in an independent GUI component, or an object that is not a view controller.

The delegate shouldn't be released because it exists during the whole life of the application.