0

我已經有了一個標準的UINavigation控制器,並像平常一樣推送我的屏幕。但是,我有一個屏幕,可以在按下按鈕時在兩個視圖控制器之間切換,因此屏幕翻轉顯示另一個視圖控制器,反之亦然。你可以隨意切換它們。修改UINavigationController損壞導航堆棧

現在,我想後退按鈕工作正常,所以我換了頂視圖控制器來實現這一如下:

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

所以,當我按回去,我必須檢查是否輸入項會丟失。彈出跳板彈回到一對夫婦的屏幕和基本要求如下:

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

然而,popViewController動畫調用什麼都不做...喜歡,就好像它從來沒有發生過。

了Donie

回答

0
@selector(myCustomBack) will not call goBack unless you left out some code. 
+0

對不起,有一種方法' - (無效)myCustomBack { \t didPressHome = NO; \t [self goBack]; }' – d0n13