修改UINavigationController会破坏导航堆栈

时间:2012-10-17 09:04:07

标签: ios sdk uinavigationcontroller navigation popviewcontroller

我有一个标准的UINavigation控制器,正常按下并弹出我的屏幕。但是,我有一个屏幕可以在按下按钮的情况下在两个视图控制器之间切换,因此屏幕翻转顶部显示另一个视图控制器,反之亦然。您可以随意切换它们。

现在,我希望后退按钮正常工作,所以我交换顶视图控制器来实现如下:

-(IBAction)switchToSlowProductEntry:(id)sender
{   
    NSLog(@"Switching to slow product entry");

    // Replace the top view with the findProductView
    FindProductView *findProdView = [ProductViewInstances shared].findProductView;
    NSMutableArray *views = [self.navigationController.viewControllers mutableCopy];

    [views removeLastObject];
    [views addObject:findProdView];

    // if sender is nil then assume we started from the viewDidLoad so no animation
    if(sender)
    {
        [UIView transitionWithView:self.navigationController.view duration:0.3 options:UIViewAnimationOptionTransitionFlipFromRight animations:^
        {
            [self.navigationController setViewControllers:views animated:NO];
        }
        completion:^(BOOL finished) {}];
    }
    else
        [self.navigationController setViewControllers:views animated:NO];

    NSLog(@"Views: %@", views);
    [views release];
    [ProductViewInstances shared].lastScreen = SlowProductEntryView;
}

-(IBAction)switchToQuickProductEntry:(id)sender
{
    NSLog(@"Switching to fast product entry");

    // Replace the top view with the findProductView
    QuickOrderEntryView *quickProductView = [ProductViewInstances shared].quickProductView;
    NSMutableArray *views = [self.navigationController.viewControllers mutableCopy];

    [views removeLastObject];
    [views addObject:quickProductView];

    if(sender)
    {
        [UIView transitionWithView:self.navigationController.view duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^
         {
             [self.navigationController setViewControllers:views animated:NO];
         }
        completion:^(BOOL finished) {}];
    }
    else
        [self.navigationController setViewControllers:views animated:NO];

    NSLog(@"Views: %@", views);
    [views release];
    [ProductViewInstances shared].lastScreen = QuickProductEntryView;
}

我有另一个屏幕的类似代码。我正在使用ProductViewInstances类来维护两个视图控制器,因为我不希望这些类被卸载,因为我在屏幕上保持舞台。

当您想要从这些屏幕向前移动时,我会像往常一样按下新屏幕。它工作,我回顾了我添加的产品后回来。如果我按回去,我会回到上面的屏幕,一切看起来都很正常。但是,当我按下自定义后退按钮时(如果按下后我需要进行处理)我遇到了问题。

popViewController什么都不做。以下是管理自定义后退按钮的基类中的代码。

-(void) viewDidLoad
{   
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil)
                                                                              style:UIBarButtonItemStyleBordered
                                                                             target:self
                                                                             action:@selector(myCustomBack)] autorelease];
    if(![ProductViewInstances shared].findProductView)
    {
        [ProductViewInstances shared].findProductView = [[FindProductView alloc] init];
        [ProductViewInstances shared].findProductView.customer = self.customer;
    }
    if(![ProductViewInstances shared].quickProductView)
    {
        [ProductViewInstances shared].quickProductView = [[QuickOrderEntryView alloc] init];
        [ProductViewInstances shared].quickProductView.customer = self.customer;
    }
}


-(void) goBack
{
    if([[ProductViewInstances shared].quickProductView checkIfItemsPending])
    {
        // Pop up dialog
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Save Entries", nil)
            message:NSLocalizedString(@"Your entries will be lost", nil)
            delegate:self
            cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
            otherButtonTitles:NSLocalizedString(@"Save", nil), nil];
        [alert show];
        [alert release];
    }
    else
    {
        // Remove rows from quick item entry screen
        [[ProductViewInstances shared].quickProductView removeRowsFromtable];
        if(didPressHome)
            [self popToSpringBoard:YES];
        else
            [self.navigationController popViewControllerAnimated:YES];
    }
}

所以当我按回来时,我必须检查这些条目是否会丢失。 SpringBoard弹出弹出几个屏幕,基本上调用以下内容:

NSArray *controllers = appDelegate.navigationController.viewControllers;
UIViewController *springboard = [controllers objectAtIndex:2];
[appDelegate.navigationController popToViewController:springboard animated:animated];

但是,popViewController动画调用什么都不做......就好像它从未发生过一样。

了Donie

1 个答案:

答案 0 :(得分:0)

@selector(myCustomBack) will not call goBack unless you left out some code.
相关问题