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