2012-04-19 4 views
17

나는 modalview로 제시하고 그 rootviewcontroller 내가했던 SecondViewController.What 할 수있는 navigationController의 rootviewcontroller을 변경하려면 어떤 점 FirstViewController.At 말을한다NavigationController의 rootviewcontroller를 변경하는 방법은 무엇입니까?

[self.navigationController initWithRootViewController:SecondViewController]; 

이다 나는 확실하지 않다 무엇을 내가 한 것은 올바른되어있는 navigationController가 FirstViewController가 출시되었는지 여부는 알 수 있습니까?이 작업을 수행하는 올바른 방법은 무엇입니까?

미리 감사드립니다.

답변

40

수행 중

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

또는 firstViewController 각각 FirstViewControllersecondViewController의 인스턴스 SecondViewController 클래스의 인스턴스

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

. 후자의 변형은 애니메이션이없는 setViewControllers:animated:의 바로 가기입니다.

+0

감사 Costique을 확인해야합니다! 작동 중입니다. setViewController가 어떻게 작동하는지 설명해 주시겠습니까? –

+4

두 가지 방법 모두 탐색 컨트롤러 내부의 전체보기 컨트롤러 스택을 대체합니다. "오래된"컨트롤러가 출시됩니다. 스택 배열은 루트 컨트롤러로 시작하고 마지막 요소는 최상위 뷰 컨트롤러입니다. – Costique

+0

@Costique 어떻게 루트를 설정할 수 있습니까? 루트를 3 번 ​​변경해야만이 방법이 효율적으로 작동 할 것인가? – Dalvik

0

이 (내가 결코 생각하지)입니다 거의 이미 초기화 된 객체에 init를 호출하지, 올바른 방법이 아니다.

난이 문제를 해결하는 방법은,은 UINavigationController의 서브 클래스를 생성하는 것이다. 이 서브 클래스에서

은, 내가 그것을 아이폰 OS가 rootviewcontroller를 설정하는 가능성을 가지고 있지에 대한 해결의의 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 실제로 아무것도하지 않는 덮어 씁니다.

또 다른 함수 (setRootViewController : aViewController)에서는 새로운 'rootviewcontroller'의 백 버튼을 숨기므로 사용자는 위조 된 rootviewcontroller를 보지 못합니다. 다음 poptorootviewcontroller가 있는지 항상이없이 배열을 반환 있도록 viewcontrollers의 게터 변경해야 스택의 인덱스 1이 아니라 인덱스 0

에 나올 수 있도록 덮어 써야

fakerootviewcontroller 위로 밀어 fakerootviewcontroller (removeobjectatindex: 0)이 도움이

희망!

+0

@Costique의 대답은 rootviewcontroller의 navigationcontroller 만 설정합니다. 이것은 스택 등 내에서 스택과 함께 떠날 것이다. 내 경우에는,이 나를 위해 작동하지 않았다 (내 tabbaritem 함께) popToRootViewController에 대한 지원이 필요합니다. – Jacco

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

+0

그러면 원래 navigationController에서 가질 수있는 모든 설정이 제거됩니다. – Antenehs

0

스위프트 3

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

    } 
관련 문제