[英]segueing between view controllers with navigation controller
Lets say i have ViewController A with 2 buttons, each is segueing to ViewController B and C (one button to View B and other to C).From ViewController CI segueing to ViewController D. All the ViewControllers are with Navigation bar so from view B i can return to A, and from view D i can return to D->C->A. 让我说我有ViewController A有2个按钮,每个按钮都是ViewController B和C(一个按钮来查看B,另一个按钮去C)。从ViewController CI到视图控制器D.所有的ViewControllers都带有导航栏所以从视图B i可以返回A,并且从视图D我可以返回到D-> C-> A. The problem is when i segueing between view D to B: the segue is performed but now in the navigation bar of B i retuned to view D and i want B will return to A like it should.
问题是当我在视图D到B之间进行切换时:执行了segue但是现在在B的导航栏中我重新调整以查看D并且我希望B将像它应该返回到A. what is the solution????
解决办法是什么????
This can be done using two ways as far as i know. 据我所知,这可以通过两种方式完成。
1) Use a custom back button on D controller 1)使用D控制器上的自定义后退按钮
2) Change the stack of navigationController ie NSArray of self.navigationController.viewControllers in the class of D controller 2)更改navigationController的堆栈,即D控制器类中的self.navigationController.viewControllers的NSArray
Use Custom back Button 使用自定义后退按钮
You can do it as follows in D controller.This is valid only if A controller is the rootViewController of navigationController.If you are keeping track of your A controller than you can use the following also 您可以在D控制器中执行以下操作。仅当A控制器是navigationController的rootViewController时才有效。如果您跟踪A控制器,则可以使用以下内容
(NSArray *)popToViewController:(UIViewController )viewController animated:(BOOL)animated (NSArray *)popToViewController:(UIViewController )viewController动画:(BOOL)动画
- (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(goBack:)]; self.navigationItem.leftBarButtonItem = backButton; self.navigationItem.hidesBackButton = YES; } -(void)goBack:(id)sender { [self.navigationController popToRootViewControllerAnimated:YES];
} }
Change the stack of navigationController 更改navigationController的堆栈
This is useful if you want to navigate to a particular controller.But the back button still displays after getting back to A controller from B,that you might have to see yourself by making some changes.Don't try to change the navigation stack in prepareForSegue 如果你想导航到一个特定的控制器,这很有用。但是从B回到A控制器后仍然会显示后退按钮,你可能需要通过做一些更改来看自己。不要尝试更改导航堆栈prepareForSegue
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *controllers = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
NSMutableSet *controllersToRemove = [NSMutableSet new];
for (id viewController in controllers) {
if (![viewController isKindOfClass:[self class]]&&![viewController isKindOfClass:[NRViewController class]]) {
[controllersToRemove addObject:viewController];
}
}
for (id controller in controllersToRemove) {
[controllers removeObject:controller];
}
self.navigationController.viewControllers = controllers;
}
Edited Code for custom back button 编辑自定义后退按钮的代码
If you want the original back Button 如果你想要原来的后退按钮
You have to use image for that with code as follow 你必须使用图像,代码如下
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 60.0f, 30.0f)];
UIImage *backImage = [[UIImage imageNamed:@"back_button_normal.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 12.0f, 0, 12.0f)];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.