UINavigationController标题重叠

时间:2012-05-16 09:43:46

标签: objective-c ios xcode uinavigationcontroller

我正在使用UINavigationController并将UIViewControllers推送/弹出。在某些情况下,我试图弹出到根视图控制器,然后在短暂延迟(0.1f)后推送视图控制器。

我的消息视图控制器的推送代码如下。我的应用会触发两个通知。第一个选择选项卡,第二个选项将正确的视图控制器推送到该选项卡的堆栈上。

//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];
}

在功能上,视图似乎弹出并正确推送但是标题没有弹出,每个新标题都叠加在旧标题之上。

我在viewDidLoad

中为每个视图控制器设置了标题
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"More";
}

如果我没有尝试将pop设置为root,然后是推迟后跟延迟 - 标题和视图按预期运行,不会发生重叠。

屏幕截图中的示例图片

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

我对堆栈溢出进行了很好的挖掘,但我看不出任何与我所拥有的问题描述相同问题的问题。

Qn.1:popToRoot,Delay,push View方法是否存在根本不正确的问题? Qn.2:如果之前有人见过这种行为,你是怎么解决的?

1 个答案:

答案 0 :(得分:1)

将延迟从0.1f增加到0.5f解决了问题

更改

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

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