2014-02-06 58 views
0

更改的UINavigationController的堆棧結果的內容我有4個項目一個UINavigationController:在碰撞

(根)mainvc - > callerlistvc - > addcallerformvc - > verifycallervc(在特定的順序)

當我在verifycallervc屏幕上時,如果我按回,我想回到callerlistvc。

這裏是抓然而,後退按鈕應該是一個系統的按鈕..所以..據我知道我不能選擇調用poptoviewcontroller更換動作:動畫(僅適用於自定義的UIBarButtonItem)

所以然後我想操縱堆棧(相當有趣和具有挑戰性!)所以這裏是我做的...

因此,目前我在verifycallervc屏幕上......並且這被調用。

- (void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 

NSMutableArray *allViewControllers = [self.navigationController.viewControllers mutableCopy]; 

__block UIViewController *mainvc = nil; 
__block UIViewController *callerlistvc = nil; 
__block UIViewController *addcallerformvc = nil; 

[allViewControllers enumerateObjectsUsingBlock:^(UIViewController *vc, NSUInteger idx, BOOL *stop) { 
    if ([vc isKindOfClass:[MainVC class]]) { 
     mainvc = vc; 
    } else if ([vc isKindOfClass:[CallerListVC class]]) { 
     callerlistvc = vc; 
    } else if ([vc isKindOfClass:[AddCallerFormVC class]]) { 
     addcallerformvc = vc; 
    } 
}]; 

[self.navigationController setViewControllers:@[ mainvc, callerlistvc, self]]; 
} 

當我這樣做後,我通常按回來,現在在callerlistvc ...很好。 不幸的是,當我按下按鈕(push-segued addcallerformvc)...它導致崩潰EXC_BAD_ACCESS。

我還試圖通過第一操縱變量callerlistvc像這樣一種不同的方法在setViewControllers方法

callerlistvc = [[UIStoryboard storyboardWithName:@"main" bundle:nil] instantiateViewControllerWithIdentifier:@"CallerListVC"]; 

在添加它之前但結果是相同的。

我增加了斷點,它是這樣的......

CallerListVC:

  1. tappedShowAddCallerListButton
  2. performSegueWithIdentifier
  3. prepareForSegue //標識字符串是正確的,destinationVC不是零

然後AddCallerFormVC: 4. viewDidLoad中 5. viewWillAppear中//性質不無

說EXC_BAD_ACCESS發生

後,我怎樣才能使這項工作?

+0

2個可選的方法來改變標準的後退按鈕行爲:1是實現UINavigationControllerDelegate方法navigationController:willShowViewController:animated:並且跟蹤你從verifycallervc彈出的時間,然後再次彈出。或看看這個問題的答案http://stackoverflow.com/questions/1214965/setting-action-for-back-button-in-navigation-controller – mbehan

回答

0

在這種情況下,更好的方法是使用自定義的UINavigationController類並擴展popViewControllerAnimated:方法。在此方法中,可以調用super方法,或者根據檢查彈出到特定的視圖控制器(超類方法)。這樣你就可以擁有你的系統導航按鈕,並且還可以控制大頭貼的位置。

+0

工程優雅! – daryl

0

不要在verifycallervc覆蓋任何東西,而不是做與正常以下回到verifycallervc

覆蓋viewWillAppear中或viewDidAppear爲addcallerformvc這樣

- (void)viewDidAppear:(BOOL)animated 
{ 
    if (![self isBeingPresented]) { 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
} 

參考: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDisplay-Notifications/RespondingtoDisplay-Notifications.html#//apple_ref/doc/uid/TP40007457-CH12-SW7

注意:未測試,現在沒有XCode ....