Whenever I press on the 'go back' button on my final detail view, it wont go back to the table view.
I have a storyboard setup like this:
UITabViewController -> UINavigationController -> UITableView -> UINavigationController -> UIView (The detailview).
When I run my program I see the back button, but clicking it does nothing.
Here is the code for DetailViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backbtn = [[UIBarButtonItem alloc] initWithTitle:#"Go Back" style:UIBarButtonItemStylePlain target:self action:#selector(gobackBtn)];
[self.navigationItem setLeftBarButtonItem:backbtn];
[self configureView];
}
- (void)gobackBtn
{
[self.navigationController popViewControllerAnimated:YES];
}
As if [self.navigationController popViewControllerAnimated:YES]; does nothing.
I'm out of ideas. Why does the backbutton not 'pop' the detailview?
Your detail view controller is the root view controller of the navigation controller. So 'popping' does nothing. You need instead to dismiss the navigation controller. Technically, you should use a delegate to tell your TableViewController to dismiss it, but I think
[self.navigationController dismissViewControllerAnimated:YES completion:NULL];
from within your goBackButton method of your detail view controller will do the trick.
My UINavigationBar is not handling properly.
I would like it to Navigate back to the last UIViewController, though i get a black screen when pressing the leftBAckButton and not the previous UIViewcontroller.
View
Controller.m viewDidLoad
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"&" style:UIBarButtonItemStyleBordered target:self action:#selector(home:)];
self.navigationItem.leftBarButtonItem = newBackButton;
This is the -(void)home:(UIBarButtonItem *)sender
-(void)home:(UIBarButtonItem *)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
Though i feel this is correct i receive this when pressing the button
As you can see the navigationbar seems to disappear and it never navigates to the previous view controller.
When using [self.navigationController popViewControllerAnimated:YES];
I receive! this on the UI
What is causing this and how can i fix it?
I have created a UINavigationBarButton where i have placed a button called "update".When update button is clicked it is navigating to SecondViewController.But the problem is in the SecondViewController TabBar is not showing.What I'm trying to do is when i tap the back button in the SecondViewController it should get back to RootViewController.Here is the code
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title=#"GooGle";
[self.navigationItem setLeftItemsSupplementBackButton:YES];
[self.navigationController setToolbarHidden:NO];
UIBarButtonItem *nextView = [[UIBarButtonItem alloc] initWithTitle:#"update" style:UIBarButtonSystemItemPlay target:self action:#selector(updateAddress)];
self.navigationItem.rightBarButtonItem = nextView;
[nextView release];
}
-(void)updateAddress
{
SecondViewController *updateView=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController presentViewController:updateView animated:YES completion:Nil];
[self.navigationItem setLeftItemsSupplementBackButton:YES];
}
When you tap the back button in SecondViewController use
[self dismissViewControllerAnimated:YES completion:nil];
Since you are not using
[self.navigationController pushViewController:youViewController animated:YES];
the navigationbar won't be visible, if you do so you will have your navigationbar and a back button by its own and while pressing the back button it will itself move to the previous view
share your results
I want to navigate to the particular page in my application and i also dont want to create any custom back button for that.If I can override the method of the navigation bar back button so I can call the poptorootviewcontroller.so i can go to specific page. Anyone knows what is the method that is called by the navigation bar button and if we can use it?
You can try this.. Write your logic in this native method.
-(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
// Your Code
}
You will have to provide the name and the implementation for the button method As there is no standard method ..
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonPressed)] autorelease];
implementation ..
-(void) backButtonPressed {
NSLog(#"Back button presses");
}
Try to use the below code:
NSArray * viewController = self.navigationController.viewControllers;
if([viewController count] > 3)
{
UIViewController * vc = [viewController objectAtIndex:0];
[self.navigationController popToViewController:vc animated:YES];
}
I have a UITableViewController that launches a UIViewController and I would like to trap whenever the back button is pressed in the child controller, which is the class that derives from 'UIViewController'. I can change the Back Button title but setting the target & action values when setting the backBarButtonItem seems to get ignored. What's a way to receiving some kind of notification that the Back button was tapped?
- (void)showDetailView
{
// How I'm creating & showing the detail controller
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"MyDetailView" bundle:nil];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Pages"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack:)];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[self.navigationController pushViewController:controller animated:animated];
[controller release];
}
- (void)handleBack:(id)sender
{
// not reaching here
NSLog(#"handleBack event reached");
}
You can implement the viewWillDisappear method of UIViewController. This gets called when your controller is about to go away (either because another one was pushed onto the navigation controller stack, or because the 'back' button was pressed).
To determine whether the view is disappearing because of the back button being pressed, you can use a custom flag that you set wherever you push a new controller onto the navigation controller, like shown below
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (viewPushed) {
viewPushed = NO; // Flag indicates that view disappeared because we pushed another controller onto the navigation controller, we acknowledge it here
} else {
// Here, you know that back button was pressed
}
}
And wherever you push a new view controller, you would have to remember to also set that flag...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
viewPushed = YES;
[self.navigationController pushViewController:myNewController animated:YES];
...
}
It has been a while since this was asked, but I just tried to do this myself. I used a solution similar to Zoran's, however instead of using a flag I did this:
- (void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear: animated];
if (![[self.navigationController viewControllers] containsObject: self])
{
// the view has been removed from the navigation stack, back is probably the cause
// this will be slow with a large stack however.
}
}
I think it bypasses the issues with flags and IMO is cleaner, however not as efficient (if there are lots of items on the navigation controller).
In my opinion the best solution.
- (void)didMoveToParentViewController:(UIViewController *)parent
{
if (![parent isEqual:self.parentViewController]) {
NSLog(#"Back pressed");
}
}
But it only works with iOS5+
I use this code:
- (void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound)
{
// your view controller already out of the stack, it meens user pressed Back button
}
}
But this is not actual when user presses tab bar button and pops to root view controller at one step. For this situation use this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(viewControllerChange:)
name:#"UINavigationControllerWillShowViewControllerNotification"
object:self.navigationController];
- (void) viewControllerChange:(NSNotification*)notification {
NSDictionary* userInfo = [notification userInfo];
if ([[userInfo objectForKey:#"UINavigationControllerNextVisibleViewController"] isKindOfClass:[<YourRootControllerClass> class]])
{
// do your staff here
}
}
Don't forget then:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:#"UINavigationControllerWillShowViewControllerNotification"
object:self.navigationController];
You can make your own button and place it as the leftBarButtonItem. Then have it call your method where you can do whatever, and call [self.navigationController popViewController... yourself
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(handleBack:)];
self.navigationItem.leftBarButtonItem = backButton;
[backButton release];
[self filldata];
[super viewDidLoad];
}
just replace backBarButtonItem with leftBarButtonItem
Simply use viewDidDisappear instead. It will be perfectly called in any scenario.
We are basing your lifecycle management on viewDidAppear and viewDidDisappear. If you know Android: the both are comparable to onResume and onPause methods. But there is a difference when it comes to locking the screen or pressing the homebutton on iOS.