uinavigationcontroller根控制器的后退按钮

时间:2011-10-10 01:50:32

标签: iphone objective-c ipad uinavigationcontroller

我有一个带有根视图控制器的UINavigationController应用程序,每次我将视图控制器推送到堆栈。假设堆栈是A B C D,其中A是根视图控制器。问题是,当我在视图控制器D并执行popToRootViewController时,它返回到A但是A上有一个后退按钮。当我单击后面时,后面只是滑入并消失,为什么会发生这种情况?

编辑:我实际上是我的UINavigationController的子类,以便我可以按如下方式设置我的rootViewController:

#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

更新更新:

所以在我设置我的rootViewController然后推送一个viewController然后尝试从该视图调用popToRootViewController之后,一切正常。但是,如果我在第二个之后推送另一个viewController然后调用popToRootViewController,现在我可以在根上看到那个奇怪的后退按钮。

1 个答案:

答案 0 :(得分: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;
}