2011-10-10 3 views
1

루트 컨트롤러가있는 UINavigationController 응용 프로그램이 있고 스택에 뷰 컨트롤러를 보낼 때마다 있습니다. 스택이 A B C D이고 A가 여기에있는 루트보기 컨트롤러라고 가정 해 봅시다. 문제는 내가보기 제어기 D에있을 때 popToRootViewController를 수행하면 A로 돌아 갔지만 A에는 뒤로 버튼이 있습니다. 뒤를 클릭하면 뒤쪽이 미끄러 져 들어가서 사라집니다. 왜 이런 일이 발생합니까?uinavigationcontroller의 루트 컨트롤러에서 돌아 가기 버튼

편집 : 다음과 같이 내 rootViewController을 설정할 수 있도록 내가 실제로 내 UINavigationController가 서브 클래스입니다 :

#import "CustomNavigationController.h" 

@implementation CustomNavigationController 

@synthesize fakeRootViewController; 

//override to remove fake root controller 
-(NSArray *)viewControllers { 
    NSArray *viewControllers = [super viewControllers];  
if (viewControllers != nil && viewControllers.count > 0) { 
NSMutableArray *array = [NSMutableArray arrayWithArray:viewControllers];   
[array removeObjectAtIndex:0]; 
    return array; } 
    return viewControllers; } 

//override so it pops to the perceived root 
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated { 
    //we use index 0 because we overrided “viewControllers” 
    ((UIViewController *)[self.viewControllers objectAtIndex:0]).navigationItem.hidesBackButton = YES; 
    return [self popToViewController:[self.viewControllers objectAtIndex:0] animated:animated]; } 

//this is the new method that lets you set the perceived root, the previous one will be popped (released) 
-(void)setRootViewController:(UIViewController *)rootViewController { 
    rootViewController.navigationItem.hidesBackButton = YES; 
    [self popToViewController:fakeRootViewController animated:NO]; 
    [self pushViewController:rootViewController animated:NO]; } 

- (void)dealloc { 
    self.fakeRootViewController = nil; 
    [super dealloc]; } 


-(id)initWithCoder:(NSCoder *)aDecoder{ 
    self = [super initWithCoder:aDecoder]; 
    if(self){ 
     UIViewController *fakeController = [[[UIViewController alloc] init] autorelease]; 
     self.fakeRootViewController = fakeController; 
     NSMutableArray *array = [NSMutableArray arrayWithArray:[super viewControllers]]; 
     [array insertObject:fakeController atIndex:0]; 
     self.viewControllers = array; 
    } 
    return self; } 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. } 

#pragma mark - View lifecycle 

/* // Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView { } 
*/ 

/* // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; } 
*/ 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; } 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); } 

@end 

더 UPDATE : 내 rootViewController을 설정 한 후 밀어 후 그래서

viewController 그리고 그보기에서 popToRootViewController를 호출하려고했는데 모두 잘 동작했습니다. 그러나 두 번째 ViewController 뒤에 다른 viewController를 push 한 다음 popToRootViewController를 호출하면 루트에서 이상한 뒤로 버튼을 볼 수 있습니다.

+0

용 - 그럼 당신은 확인해야? – bdares

+0

나는 그렇게 생각하지 않는다. – adit

+0

루트로 뛰어 오르고, 여분의 뒤로 버튼을 없애고, 스택으로 돌아가서 루트로 다시 팝업하는 경우, 다시 버튼이 여전히 존재 하는가? 그렇지 않다면 초기화시 스택에 A를 밀어 넣는 것입니다. – bdares

답변

0

나 역시 같은 문제에 직면 해있다. 따라서 루트 컨트롤러에서 leftBarButtonItem을 nil과 같게 지정하십시오.

self.navigationItem.leftBarButtonItem = nil; 

루트 컨트롤러가 프로그램에서 재사용되는 경우. 당신이 스택에 다른보기를 밀어 전에 당신이 (다시) 스택에 밀어있어 가능>

BOOL needBackBarButton = (1 < [self.navigationController.viewControllers count]) ? YES : NO ; 
if (! needBackBarButton) 
{ 
    self.navigationItem.leftBarButtonItem = nil; 
} 

를 다른 컨트롤러

if (needBackBarButton) 
{ 
      // Create custom navigationItem here. 
} 
else 
{ 
    self.navigationItem.leftBarButtonItem = nil; 
} 
+0

그런데 어떻게 내가 필요한 단추를 다시 만들 수 있을까? – adit

+0

다른 컨트롤러에 대해서 생각할 필요가 없습니다. 그냥 self.navigationItem.leftBarButtonItem = nil을 추가하십시오. 이 귀하의 루트 Viewcontroller의 viewDidLoad 메서드입니다. 이 문제가 해결되기를 바랍니다. –

+0

나는 그것을 가지고 있었고 그것은 어떤 것도 변화시키지 않았다. – adit