简体   繁体   English

按下导航控制器后退按钮时如何导航到其他View Controller?

[英]How to navigate to some other View Controller when Navigation Controller back button is pressed?

I have a viewController in which, on the click of back button, I need to go to a specific viewController and call its method.So I created a barButton and added as a BACK button in navigation bar.When its selector is called I can see only see a black screen, nothing else. 我有一个viewController在其中单击后退按钮,我需要转到特定的viewController并调用其方法,因此我创建了一个barButton并添加为导航栏中的BACK按钮。当其selector被调用时,我可以看到只看到黑屏,没别的。

Here how I am doing it. 这是我的做法。

In viewDidLoad viewDidLoad

  //Back Button in navigation Bar.
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(navigationBackButtonClicked:)];
self.navigationItem.leftBarButtonItem=newBackButton;

The selector below executes and shows a black screen. 下面的selector执行并显示黑屏。

  -(void)navigationBackButtonClicked:(UIBarButtonItem *)sender {
   SharedManager *sharedManagerObject = [SharedManager sharedInstance];

   NSString *source = sharedManagerObject.toCityString;
   NSString *destination =  sharedManagerObject.fromCityString;
   NSString *dateStr = sharedManagerObject.dateSelected_String;

   BusListViewController *buslist_VC = [self.storyboard instantiateViewControllerWithIdentifier:@"BusListViewController"];


   [buslist_VC getBusListForSource:source destination:destination date:dateStr];

   [self.navigationController popToViewController:buslist_VC animated:YES];

 }

You need to add your buslist_VC to the view hierarchy of your navigation controller before using [popToViewController:animated:]. 您需要将添加buslist_VC使用之前,你的导航控制器的视图层次[popToViewController:动画:]。 This is used to display some viewcontrollers already in the stack of your navigation Controller. 这用于显示导航控制器堆栈中已经存在的某些视图控制器。

Either way what you're asking might be a weird behaviour for your user but you can use : 无论哪种方式,您要问的问题都可能对您的用户来说很奇怪,但是您可以使用:

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count - 1] animated:NO];
[self.navigationController pushViewController:buslist_VC animated:NO];

You cann't back to a new ViewController, you can push a new ViewController, in your code change this: 您不能返回到新的ViewController,可以推送新的ViewController,在代码中更改以下内容:

[self.navigationController pushViewController:buslist_VC animated:YES];

But keep in mind, you are putting this new viewController inside the other (where you are created a newBackButton), and the default back button come back to this. 但是请记住,您正在将这个新的viewController放在另一个内部(在其中创建了newBackButton),默认的后退按钮返回到此位置。 If you want to come back to the root use this: 如果您想回到根源,请使用以下命令:

   [self.navigationController popToRootViewControllerAnimated:YES];

Or, now you can come back to any viewController on the navigation stack, this array: 或者,现在您可以回到导航堆栈上的任何viewController这个数组:

   NSArray *navStacks = self.navigationController.viewControllers;

Using popToViewController: be sure is in the stack. 使用popToViewController:确保已在堆栈中。

You should use 你应该用

NSArray *navStacks = self.navigationController.viewControllers;

select needed view controller and do 选择所需的视图控制器并执行

[self.navigationController popToViewController:selectedVC animated:YES];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查视图控制器是否在导航控制器的其他页面按下“后退”按钮后出现? - how to check if a view controller appeared after some other page in navigation controller pressed back button? 在导航控制器中按下“后退”按钮时,是否可能不取消分配视图控制器? - is it possible to not dealloc a view controller when the back button is pressed in a navigation controller? 当按下导航后退按钮时,将值返回到呈现视图控制器 - Return values to presenting view controller when navigation back button pressed 迅速按下导航栏后退按钮时重新加载视图控制器2 - Reloading view controller when navigation bar back button is pressed in swift 2 按下indexPath单元格时如何导航或显示其他视图控制器 - how to navigate or show other view controller when indexPath cell pressed 当导航栏的内置后退按钮被按下时弹出到根视图控制器 - Pop to the root view controller when the navigation bar's built in back button is pressed Xcode:检测何时按下导航控制器实现的“后退按钮” - Xcode: Detecting when a navigation controller implemented “back button” is pressed 当按下其他视图 controller 中的按钮时,视图 controller 中的点击手势导致视图 controller 的打开延迟 - Tap gesture in a view controller causing delay in opening of the view controller when a button in other view controller is pressed 标签栏为初始视图控制器时,如何使“返回”按钮出现在导航控制器中 - How to make 'Back' button appear in Navigation Controller when Tab Bar is initial view controller 按下导航控制器的左键按钮时如何停止视图消失 - how to stop view disappearing when navigation controller left bar button pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM