开发者

back button at root controller of uinavigationcontroller

开发者 https://www.devze.com 2023-04-12 01:15 出处:网络
I have a UINavigationController application with a root view controller and each time I am pushing a view controller to the s开发者_C百科tack. Let\'s say the stack is A B C D where A is the root view

I have a UINavigationController application with a root view controller and each time I am pushing a view controller to the s开发者_C百科tack. Let's say the stack is A B C D where A is the root view controller here. The issue is that when I am at view controller D and do a popToRootViewController it went back to A but A has a back button on it. When I click on back the back just slides in and disappear, why is this happening?

EDIT: I am actually subclassing my UINavigationController so that I can set my rootViewController as follows:

#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

MORE UPDATE:

So after I set my rootViewController and then push a viewController and then tried to call popToRootViewController from that view, it all worked fine. However, if I push another viewController after the second one and then call the popToRootViewController, now I can see that weird back button on the root.


I too face the same problem. So in your root controller assign leftBarButtonItem equal to nil.

self.navigationItem.leftBarButtonItem = nil;

If your root controller reusing in the program. Then you need to check ->

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

For other controllers

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号