[英]back button at root controller of uinavigationcontroller
I have a UINavigationController application with a root view controller and each time I am pushing a view controller to the stack. 我有一个带有根视图控制器的UINavigationController应用程序,每次我将视图控制器推入堆栈时。 Let's say the stack is ABCD where A is the root view controller here.
假设堆栈是ABCD,其中A是此处的根视图控制器。 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.
问题是,当我在视图控制器D上执行popToRootViewController时,它返回到A,但是A上有一个后退按钮。 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: 编辑:我实际上是我的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
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. 因此,在我设置了rootViewController并推送了一个viewController并尝试从该视图调用popToRootViewController之后,一切正常。 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.
但是,如果我在第二个viewController之后推另一个viewController,然后调用popToRootViewController,现在我可以看到根上的那个奇怪的后退按钮。
I too face the same problem. 我也面临同样的问题。 So in your root controller assign leftBarButtonItem equal to nil.
因此,在您的根控制器中,将leftBarButtonItem分配为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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.