I wonder what is the function is called when the back button is pressed on the navigationBar.
I want to add some functionality when the button is pressed, who knows it?
Thanks in advance
The functionality you want is in the UINavigationBarDelegate protocol. Implement the -navigationBar:shouldPopItem: method and set your class as the delegate of the navigation bar in question.
Assuming you're referring to native controls, there's no way to do quite what you want, just using the built-in stuff. What you want to do is create a 'fake' back button, and stick it up in the left side of the navigation bar. Then you can set its target and action to whatever you like.
I suppose you're talking about the back button that automatically is added to a UINavigationBar when you push a new viewcontroller on a navigationcontroller.
The default action for the back button is to pop the current viewcontroller from the navigation stack and return to the previous viewcontroller.
If you want to define a custom behaviour to the backbutton you'll have to create a new button and tie a selector to it's action property:
//Create a new barbutton with an action
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleDone target:self action:#selector(doSomething)];
// and put the button in the nav bar
self.navigationItem.leftBarButtonItem = barbutton;
[barbutton release];
Edit:
//An example on how the doSomething method could be implemented.
-(void) doSomething
{
//Do your custom behaviour here..
//Go back to the previous viewcontroller
[self.navigationController popViewControllerAnimated:YES];
}
Related
In Navigation Bar you get a Arrow shaped Back button similar to this image. (According to this image the arrow is named folders)
I need to add this Arrow shaped bar button on to a cell. Is this possible ? How can i do this programatically ?
The navigation controller back button (with the arrow appearance) can only be added using code to a navigation bar - by setting the back bar button property of the navigation item of the previous controller in the stack.
You can't add this button to a table cell, and frankly it would look a little odd - what does it do? Does each cell go "back" to a different screen? Apple aren't usually keen on you repurposing existing UI elements to mean something other than the standard.
There is no built in UIButton or UIBarButtonItem that will give you that appearance, therefore you will have to create your own image and use that as the background for a custom button which you can then add to your cell.
You can take a custom button and can set background as an image which is arrow shaped.
if you are using navigation controller-
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[[self navigationController] pushViewController:myViewController animated:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Folders" style:UIBarButtonItemStyleBack target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[myViewController release];
You can take custom table cell to prepare table view. In that custom table cell you will be able to add button and IBaction. You can take help from this tutorial on how to create UITableCell and then UITableView.
I'm trying to change the text of the back button from within Interface Builder, but when I select the Navigation Bar and go into the Attributes Inspector and set the Back Button text to "Close" it still shows up with the title of the previous view.
How do I get this to work?
The back button will always show the previous UIViewController title or backBarButtonItem defined.
So if you have "view1" and move to "view2" you need to set the backButton in "view1" so it's displayed correctly while "view2" is presented.
(In other word "view1" is responsible to provide what should be displayed in a back button that point to it)
In this way if "view1" is follow by any views all those will have the correct back button.
Try this in -(void)viewDidload, as this method is fired after the nib has loaded:
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
A quick and dirty trick is to set the "title" to what name you want in the button in the willDisappear override in the view controller. Make sure to set the "title" back to the correct name on the willAppear override view controller. One drawback with this technique is that on slower units you can see it change. On iPhone 4 and later it's hardly noticeable.
Hope this helps.
Here is what I did, this is a dynamic questionary application;
-Created a UINavigationBased application
-Created some new UIVIewController subclasses with their xib's and designed them.
-I opened mainwindow.xib and set the navigationcontroller to has a navigation bar and a toolbar in black.
-So now when I switch back and forth between views by pushing them with the navigation controller I can see that shared toolbar and a navigationbar in every page with a backbutton, which is cool.
some of my uiviews are tableviews and some are textboxes
What I want is I want to put a "next" button on that toolbar and be able to go through my views but not with getting the row tap. With each next click I will decide which view to push.
How can I put this "shared" button? and in which class can I define its functionality? also I want to make it sometimes invisible for instance on first page and on last page of questionary.
In every page that you want to have a next button add this in the init or viewDidLoad
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Next"
style:UIBarButtonItemStylePlain
target:self
action:#selector(pushNextView)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
Now just implement pushNextView method to push what ever view you want
-(void)pushNextView{
//push next view
}
Edit
By specifying the target of your rightButton to AppDelegate (right now it points to self) you can setup the pushNextView method in your AppDelegate and manage all your views there.
I have a navigation view controller and there are 3 view controllers in the navigation stack. Now on the third and top most visible view controller I have a default back button coming in.
I need to bring this view controller in edit mode which I did... Now the requirement is to have a cancel button as the left bar button item instead of back button.
This is similar to the functionality given by contacts application of iPhone where you edit a particular contact.
Any clue how to achieve this?
To hide back button and add a left bar button use-
[self.navigationItem setHidesBackButton:TRUE];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector()];
[self.navigationItem setLeftBarButtonItem:leftBarButton];
[leftBarButton release];
And then to programmatically return to the previous view controller, you can do-
[self.navigationController popViewControllerAnimated:YES];
This is a simpler way:
[self.navigationItem setHidesBackButton:YES];
If you are using storyboard, you can also just drag a bar button item onto the navbar where the back button would normally show up. This will override it.
I have a reference to a "UIBarButtonItem", is there a way I can add a custom "Back Navigation Button" to that item when it is not part of a Navigation based view?
I can add a left button:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Custom Back"
style:UIBarButtonItemStylePlain target:self
action:#selector(backAction:)];
menuItem.backBarButtonItem = backButton; //This doesn't seem to work.
menuItem.popOverNavigationItem.leftBarButtonItem = backButton; //This shows a normal button
So how could I make the leftmost button look like a back navigation button?
UPDATE: This other question answered the root of my problem that was leading me to try and do this non-standard UI setup:
iPad: Merge concept of SplitViewController and NavigationController in RootView?
I think the correct way to set the back button is to set it for the view controller that you would be going back to. For example:
RootViewController > DetailViewController
If you want the back button to say "Custom Back" whilst you're on DetailViewController, you have to actually set RootViewController's back button to "Custom Back".
Hope that makes sense.
Having just struggled with this: I think the answer is “no.” You'll need a controller hierarchy.