Damo
Damo

Reputation: 12890

UINavigationController Title Overlap

I am using a UINavigationController and pushing/popping UIViewControllers onto it. In some instances I am attempting to pop to the root view controller and then push a view controller after a short delay (0.1f).

My push code for the Message View Controller is as follows. My app fires two notifications. The first to select a tab and the second to push the correct view controller onto the stack of that tab.

//user taps a button and the app needs to switch tab and push the correct viewController
//onto the tab. I have tried setting pop == NO to avoid a 'double pop' but I still get
//overlapped titles
-(IBAction)messages:(id)sender {
    NSDictionary* dictionary = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSNumber numberWithInt:4], [NSNumber numberWithBool:YES] , nil] forKeys:[NSArray arrayWithObjects:@"tab",@"pop", nil]];
    [[NSNotificationCenter defaultCenter] postNotificationName:kAutoSelectTab object:dictionary]; 

    [[NSNotificationCenter defaultCenter] performSelector:@selector(postNotificationName:object:) withObject:kMessages afterDelay:0.1f];
}


//responds to the first notification
-(void)autoSelectTab:(NSNotification*)notification {

    NSDictionary* dictionary = (NSDictionary*)[notification object];

    int tab = [[dictionary objectForKey:@"tab"] intValue];
    BOOL pop = [[dictionary objectForKey:@"pop"] boolValue];

    [self.tabBarController setSelectedIndex:tab];

    UIViewController* vc = [[self.tabBarController childViewControllers] objectAtIndex:tab];
    PSLogDebug(@"Selecting tab:%@",[vc class]);
    [self tabBarController:self.tabBarController didSelectViewController:vc];

    if (pop == YES) {

        if ([vc isKindOfClass:[UINavigationController class]]) {
            [(UINavigationController*)vc popToRootViewControllerAnimated:YES];
        }
    }
}

//responds to the second notification
-(IBAction)messages:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:NO];
    MessagesViewController* vc = [[MessagesViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

Functionally the views appear to pop and push correctly BUT the titles do not pop and each new title is overlaid atop the old one.

I set the titles for each of the view controllers in viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"More";
}

When I don't attempt the pop to root followed by the delay followed by the push - the titles and views behave as expected with no overlapping occurring.

Example Images from screenshots

enter image description here enter image description here enter image description here

I've had a good dig around stack overflow but I can't see any questions which describe the same issue as the one I am having.

Qn.1: Is there something fundamentally incorrect with the popToRoot, Delay, push View approach? Qn.2: If anyone out there has seen this kind of behaviour before, how did you resolve it?

Upvotes: 2

Views: 946

Answers (1)

Damo
Damo

Reputation: 12890

Increasing the delay from 0.1f to 0.5f fixed the problem

Change

[[NSNotificationCenter defaultCenter] performSelector:@selector(postNotificationName:object:) withObject:kMessages afterDelay:0.1f];

to

[[NSNotificationCenter defaultCenter] performSelector:@selector(postNotificationName:object:) withObject:kMessages afterDelay:0.5f];

Upvotes: 1

Related Questions