2

我正在使用 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";
}

当我不尝试弹出到根目录然后延迟再推送时 - 标题和视图的行为符合预期,没有发生重叠。

来自屏幕截图的示例图像

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

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

Qn.1:popToRoot、Delay、push View 方法是否存在根本错误?Qn.2:如果有人以前见过这种行为,你是如何解决的?

4

1 回答 1

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];
于 2012-12-06T11:51:54.050 回答