2012-04-19 80 views
17

我有是作爲modalview,其RootViewController的是說FirstViewController.At某些時候,我想改變navigationController的RootViewController的到SecondViewController.What我所做的是如何更改NavigationController的rootviewcontroller?

[self.navigationController initWithRootViewController:SecondViewController]; 

我不知道我所做的是正確的navigationController以及FirstViewController是否被釋放?請任何人知道這是做什麼的正確方法?

在此先感謝!

回答

40

執行下列任

[firstViewController.navigationController setViewControllers: [NSArray arrayWithObject: secondViewController] 
                animated: YES]; 

firstViewController.navigationController.viewControllers = [NSArray arrayWithObject: secondViewController]; 

其中firstViewControllerFirstViewControllersecondViewController一個實例是SecondViewController類的實例,分別。後一種變體是沒有動畫的setViewControllers:animated:的快捷方式。

+0

謝謝Costique!它工作。請你解釋setViewController是如何工作的? – 2012-04-19 11:27:36

+4

這兩種方法都取代了導航控制器內的整個視圖控制器堆棧。 「舊」控制器獲得釋放。堆棧數組以矩陣控制器開始,其最後一個元素是最頂層的視圖控制器。 – Costique 2012-04-20 05:09:52

+0

@Costique如何設置Root ...假設我必須更改根3次,那麼這種方法是否可以有效地工作? – Dalvik 2015-04-30 13:57:41

0

這是不正確的方式,很少(我想永遠不會)調用init已經初始化的對象。

我解決這個問題的方法是創建UINavigationController的子類。

在本小類中,我覆蓋initwithrootviewcontroller:

- (id) initWithRootViewController:(UIViewController *)rootViewController 
{ 
    UIViewController *fakeController = [[[UIViewController alloc] init] autorelease]; 

    self = [super initWithRootViewController:fakeController]; 
    if(self) 
    { 
     self.fakeRootViewController = fakeController; 
     rootViewController.navigationItem.hidesBackButton = YES; 

     [self pushViewController:rootViewController animated:NO]; 
    } 
    return self; 
} 

的fakeRootViewController實際上什麼也不做,這是爲iOS沒有可能設置RootViewController的一種變通方法。

在另一個函數(setRootViewController:aViewController)中,您隱藏了新的'rootviewcontroller'的後退按鈕,因此用戶從不會看到有假的rootviewcontroller。然後將其推的fakerootviewcontroller

的poptorootviewcontroller應覆蓋,以確保它總是彈出到堆棧的索引1,索引不0

viewcontrollers的吸氣劑應改變,從而它返回一個數組沒有上述fakerootviewcontroller(removeobjectatindex: 0

希望這有助於!

+0

@Costique的答案只設置rootviewcontroller的navigationcontroller。這會給你一個棧內的堆棧等。在我的情況下,我需要支持popToRootViewController(帶有tabbaritem),所以這不適合我.. – Jacco 2012-04-19 10:12:12

0

您需要進行自定義的UINavigationController

@interface mySwitchRootViewNavigationController() 

@property (nonatomic, retain) myFirstViewController * FirstViewController; 
@property (nonatomic, retain) mySecondViewController * SecondViewController; 

@end 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.FirstViewController = [[myFirstViewController alloc] init]; 
    self.SecondViewController = [[mySecondViewController alloc] init]; 
} 

-(void) setRootViewControllerWithID:(int) viewControllerID 
{ 
    if (viewControllerID == 1) { 
    self.viewControllers = [NSArray arrayWithObject:self.SecondViewController]; 
    } else 
    { 
    self.viewControllers = [NSArray arrayWithObject:self.FirstViewController]; 
    } 
} 

-(void)viewWillAppear:(BOOL)animated 
{ 
    [self setRootViewControllerWithID:intVar]; 
    [super viewWillAppear:animated]; 
} 

初始化

mySwitchRootViewNavigationController * switchView = [mySwitchRootViewNavigationController alloc] init]; 
1
- (void) changeRootViewControllerOFNavigationControlllerAtRuntime:(UIViewController *) viewController { 

    UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:viewController]; 
    [UIApplication sharedApplication].delegate.window.rootViewController=navController; 
}  
+4

寫一些解釋你的答案的內容。 – Hemang 2016-01-20 09:18:41

+0

這將刪除您在原始navigationController中可能擁有的所有設置。 – Antenehs 2016-04-01 21:41:10

0

斯威夫特3

fileprivate func changeRootVC() { 
     if let newVC = self.storyboard?.instantiateViewController(withIdentifier: "MyStoryboardID"), let nc = self.navigationController { 
      nc.setViewControllers([newVC], animated: true) 
     } 

    } 
相關問題