0

我被困在試圖定製UINavigationController的後退按鈕。 在RootViewController中,我在viewDidLoad中設置了self.title,並且此字符串出現在導航欄中。在-didSelectRowAtIndexPath我創建子視圖控制器,配置後退按鈕並調用-pushViewController。對子進行處理會將子視圖控制器推入堆棧;我需要後退按鈕彈出到初始視圖,就像從第一個子視圖控制器返回時一樣。當前的後退將彈出到先前的視圖,所以如果堆棧中有5個子視圖控制器,我必須按回5次按鈕才能進入根視圖。 當後退按鈕顯示時,我無法取消操作。在子VC中,我能夠popToRootViewController;然而,現在回滾按鈕出現在根視圖(!)上,我必須再次按下回退按鈕才能恢復原始標題並移除後退按鈕。 這裏是根-viewDidLoad的一部分:UINavigationController popToRootViewController不重置標題,清除返回按鈕

- (void)viewDidLoad { 
    self.title = @"My Nav Bar Title";  // displays on root navigation bar title 
    // some setup code... 
    [super viewDidLoad]; 
} 

這裏是-didSelectRowAtIndexPath,其中,選擇在子視圖的tableview細胞結果被推到堆棧的一部分:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    ChildVC *child = [[ChildVC alloc] 
          initWithNibName:@"Child" 
          bundle:nil]; 

    [self.navigationController dismissModalViewControllerAnimated:YES]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Quiz" style:UIBarButtonItemStylePlain target:self action:@selector(backToMenu)]; 
    self.navigationItem.backBarButtonItem = backButton; 
    [backButton release]; 

    [self.navigationController pushViewController:child 
              animated:YES]; 
    [child release]; 
} 

這裏是沒有按操作方法「T火的時候,按下後退按鈕:

-(void)backToMenu { 
    NSLog(@" in root backToMenu"); 
    [self.navigationController popViewControllerAnimated:YES]; 
} 

ChildVC也將創造在其-didSelectRowAtIndexPath一個新的子和推新Child C級ontroller,作爲下一子頁面「:

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    Child *newChild = [[Child alloc] 
          initWithNibName:@"Child" 
          bundle:nil]; 

    [self.navigationController dismissModalViewControllerAnimated:YES]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    self.title = self.quizString; // child view correctly displays customized title 

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
            initWithTitle:@"Quiz" 
            style:UIBarButtonItemStylePlain 
            target:self 
            action:@selector(backToMenu)]; 
    self.navigationItem.backBarButtonItem = backButton; 
    [backButton release]; 

    [self.navigationController pushViewController:newQuestion 
             animated:YES]; 
    [newChild release]; 
} 

兒童-viewWillDisappear我設置一個全局變量,所以我知道什麼時候,當彈出回根推新子和:

-(void)viewWillDisappear:(BOOL)animated { 
    [super viewWillDisappear:YES]; 
    if (startOver) { 
      [self backToMenu]; 
    } 
} 

Child -backToMenu: 
-(void)backToMenu { 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

這裏當返回按鈕被按下子序列:

- 兒童-viewWillDisappear被調用,調用-backToMenu - -backToMenu調用popToRootViewControllerAnimated: - 兒童-viewWillDisappear再次調用,調用-backToM ENU - 根-viewWillAppear調用 - 控制返回到兒童-backToMenu

根視圖顯示正確,但導航欄包含後退按鈕和標題一樣,它仍然是一個子視圖。按下後退按鈕可刪除後退按鈕,恢復原始標題。

我該如何做這項工作?理想情況下,我想只在堆棧中有1個子視圖,但我無法弄清楚如何;那麼後退按鈕將返回到根視圖。但是當我嘗試這樣做時,我得到NSInvalidArgumentException',原因:'不止一次推送相同的視圖控制器實例...'

另外,任何顯而易見的原因爲什麼按鈕時按鈕不會觸發操作?任何幫助非常感謝... thx

回答

0

uhm,你的backButton調用popToRootViewController,它調用viewWillDissapear,其中,如果startOver爲true,則調用popToRootViewController AGAIN? 無論如何它會發生什麼?它繼續叫早些時候popToRootViewController ...

backButton->popToRoot->viewWillDissapear->check startOver 
->YES->popToRoot->viewWillDissapear again->check startOver again->?? 
->NO->continue the disappearing of the view that was called also by popToRoot 

是不是,如果有多餘的,因爲這兩個它的分支機構之前繼續popToRoot或再打電話popToRoot?

爲什麼不先測試startOver(在你的backToMenu中),然後popToRootViewController如果爲true?

0
UIBarButtonItem *btnBack=[[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStyleDone target:self action:@selector(back:)] ; 
    self.navigationItem.leftBarButtonItem=btnBack; 
    //Add image on back button 
    UIImage *backButtonImage = [[UIImage imageNamed:@"btn_back.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)]; 
    [btnBack setBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

將此代碼放在您的視圖控制器要彈出根視圖控制器[initWithNibName method]形式

- (void) back : (id)sender 
{ 

[self.navigationController popToRootViewControllerAnimated:YES]; 

}