0

My application fit inside a UINavigationController. Simply I setted up a MyUIViewController as rootViewController of navigationController and than I set up a customTitleView for the navigationItem of myViewController inside viewDidLoad.

Now when I push a newViewController I expect to see the previous customTitleView (as described by Apple NavigationController reference) but it doesn't.

What's wrong?

A little part of code below and the Apple UINavigationController Reference

"If the new top-level view controller has a custom title view, the navigation bar displays that view in place of the default title view. To specify a custom title view, set the titleView property of the view controller’s navigation item."

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitleView:customTitleView];
}

Maybe the "default title view" means "nothing"? I interpreted it as the previous titleView.

EDIT

I'm trying a work around but I have some other issues. The work around is:

In every ViewController that I push inside the NavigationController I setup in viewDidLoad the titleView getting it from the rootViewController

UiView *originalContainer = [[[self.navigationController.viewControllers objectAtIndex:0] navigationItem] titleView]

I create a newTitleView and I put inside the originalContainer.subviews (UIButton) cause I need the Target action from it.

All works perfectly for the first pushed controller but if I push another viewController in the stack (the second one pushed) I loose every reference to the navigationController. Every instance variables are nil.

This values are getted inside the viewDidLoad

firstViewControllerPushed.navigationController = (UINavigationController*)0x6693da0 firstViewControllerPushed.parentViewController = (UINavigationController*)0x6693da0

secondViewControllerPushedFromFirstViewControllerPushed.navigationController = 0x0 secondViewControllerPushedFromFirstViewControllerPushed.parentViewController = 0x0

It seems that the secondViewControllerPushed lives nowhere!!

How it's possible?

I double checked that I correctly push the viewController instead of present it modally

Couse this issue I'm not able to make a right setup of the newTitleView for the secondViewControllerPushed.

Community
  • 1
  • 1
Gabriele
  • 1,163
  • 1
  • 11
  • 24

1 Answers1

2

This is a hack in a half but I feel it gives you a bit more control in the viewController you are working in. So below is a small method I have setup and then I just call it in viewDidLoad - [self setUpNavBar];

And using [UIButton buttonWithType:101] is perfectly fine as it passed Apple Validation.

- (void) setUpNavBar
{
    [self.navigationController setNavigationBarHidden: NO animated:YES];

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.title = @"Settings";


    UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [backButton setTitle:@"Back" forState:UIControlStateNormal];

    UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backItem;
    [backItem release];

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Logout" 
                                                                            style:UIBarButtonItemStylePlain target:self 
                                                                            action:@selector(logoutAction)] autorelease];                     

}
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
Jason Jardim
  • 15
  • 1
  • 2
  • i was trying a work around similar to yours but I'm experiencing some issue. I updated the question. – Gabriele Nov 21 '11 at 17:53