简体   繁体   English

使用导航控制器在视图控制器之间进行隔离

[英]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.

相关问题 在导航控制器中重新选择视图控制器时,整个屏幕为空白 - When segueing back to view controller in navigation controller, entire screen is blank 筛选到视图控制器及其内容显示之间的差距-UIDocument - Gap between segueing to view controller and its contents showing - UIDocument 视图控制器之间的导航不起作用 - Navigation between view controllers is not worked iOS在视图控制器和导航控制器之间进行选择 - iOS segue between view controllers with navigation controllers 在没有导航控制器的情况下关闭以前的视图控制器的同时转到新的视图控制器:swift - Segueing to New View Controller while Dismissing previous without navigation controller: swift 在iOS上进行排序时将元素保留在Navigation Controller中 - Keep elements in Navigation Controller when segueing on iOS 将视图控制器导入另一个控制器进行segueing是否安全? - Is it safe to import a view controller to another controller for segueing? Swift Xcode 在视图控制器之间切换 - 在视图控制器之间导航 - Swift Xcode Switching between View Controllers - Navigation between View Controllers Swift:如何在视图控制器之间进行切换,并使用导航栏在子视图控制器中向后移动(XLPagerTabStrip) - Swift: How to segue between view controllers and use navigation bar to go backwards within a child view controller (XLPagerTabStrip) 组合选项卡,导航控制器和视图控制器 - Combine Tab,Navigation Controller and View Controllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM