I have a UINavigationController and I have to keep the the default back button "the back arrow style" I just want to ask if I can change the back button action without build new one and change its style
AFAIK you cannot change the action of the default back button itself but you can place a UIBarButtonItem as leftBarButtonItem there and assign your own action.
If there is a leftBarButtonItem defined then this is shown and not the default back button.
However, keep the GUI guidelines in mind when doing tricks like this.
No. If you want a custom back button, you have to create a custom UIBarButtonItem, then assign it to the appropriate property:
self.navigationItem.backBarButtonItem = myCustomBackItem;
The back button in UINavigationBar is generated automatically as soon as u Push a new UIView. In order for you to customize the Back button is to Create a new UIToolBar + a UIBarButtonItem with custom view.
Code below is the sample to use a custom UIBarButtonItem in UIToolBar.
// create button
UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
// create button item -- possible because UIButton subclasses UIView!
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
// add to toolbar, or to a navbar (you should only have one of these!)
[toolbar setItems:[NSArray arrayWithObject:backItem]];
navItem.leftBarButtonItem = backItem;
Link below is the design of iOS buttons in PSD format for further modifications.
http://www.chrisandtennille.com/pictures/backbutton.psd
You can make custom button and can make action on it but you can not change default backButton action.....
self.navigationItem.leftBarButtonItem = getBackBtn;
The UINavigationController sends a message to it's delegate when it pushes and pops a ViewController.
You can find out when the back button gets pressed by implementing the following and adding <UINavigationControllerDelegate> in your .h file
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.delegate = self;
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
self.navigationController.delegate = nil;
}
-(void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated{
//Test here if the View Controller being shown next is right below the current
// ViewController in the navigation stack
//
//Test by:
// 1. comparing classes, or
// 2. checking for a unique tag that you previously assigned, or
// 3. comparing against the [navigationController viewControllers][n-2]
// where n is the number of items in the array
if ([viewController isKindOfClass:NSClassFromString(#"ViewControllerClassThatGetsPushedOnBACK")){
//back button has been pressed
}
if (viewController.tag == myUniqueTagIdentifier){
//back button has been pressed
}
if ([navigationController.viewControllers[navigationController.viewControllers.count-2]==viewController]){
//back button has been pressed
}
}
Apple Docs UINavigationController Class Reference:
The root view controller is at index 0 in the array, the back view
controller is at index n-2, and the top controller is at index n-1,
where n is the number of items in the array.
Related
I have created a utility app that links by button to another xib called scene - I am trying to create a navigation control for that link. When the button is clicked to then have a 'back' button on my scene xib. I don't wish to have a navigation bar visible on the Main View Controller or the Flipside View Controller. I'm quite new to iOS and I have no idea how to do this?
Would it maybe just be better to have a button going back to menu on a custom HUD? I don't know if that can be done?
Thank you for any help in advance, and thank you for your time
you could create a custom UINavigationBar on your scene xib, and add the custom back button to it if you don't want to create NavigationController , alternate would be that you could just make your first view as NavigationController and push the Scene view over it and it will brings the back button on the child view which is scene, keep your navigationBar hidden when you are on MainViewController and show only on scene view.
For hide UINavigationBar:
[[self navigationController] setNavigationBarHidden:YES animated:YES];
And Your can crate Custom UIButton and put anywhere (As Your requirement).
and in its method, Write code for go back to UIViewController (previous UIVieController).
Such like,
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
btnBack.frame = CGRectMake(10, 10.5, 36, 39); // change it , As your wish
[btnBack setBackgroundImage:[UIImage imageNamed:#"MMBack.png"] forState:UIControlStateNormal];
[self.view addSubview:btnBack];
// call method of UIButton
-(void)goBack:(UIButton *) Sender
{
[self.navigationController popViewControllerAnimated:YES];
}
I'm having a curious issue with backBarButtonItem. I want to replace its title in the entire application for "Back" and replacing the back button works in most of -viewDidLoad event but in other views it's not working and show the name of the previous view. Has someone has the same problem?
P.S. The way to replace the backBarButtonItem is the standard one instantiating an UIBarButtonItem and setting it to viewController.navigationIten.backBarButtonItem property.
The backBarButtonItem does not set the back button that is shown in the current view, it sets the back button that navigates to the current view, i.e. the back button in the next view.
This makes sense because the back button's title is usually the title of the previous view controller.
If you want to set the left button in the navigation bar directly, use self.navigationItem.leftBarButtonItem.
when you push the view from your current view at that time after allocate your next viewcontroller object ,just put bellow line
YourViewController *objView = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
self.navigationItem.title=#"Back";
[self.navigationController pushViewController:objView animated:YES];
your Next View will Appear with Back Button....
:)
Well, at last I've found the solution to this issue.
If you want that any backBarButtonItem of your application has the same title a good approach is to subclass UINavigationController and override - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated to replace the back button.
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"BackButtonLabel", "")
style:UIBarButtonItemStyleDone
target:nil
action:nil];
viewController.navigationItem.backBarButtonItem = _backButton;
_backButton = nil;
[_backButton release];
[super pushViewController:viewController animated:animated];
}
By this way every back button in your application will have the same title.
I hope this will be helpful for anyone else.
I want to make a done button appear UINavigationBar when any row in my UITableView is selected and I want this button to perform an action performSegueWithIdentifier.
Any ideas on how to implement it?
Add the following to your tableView:didSelectRowAtIndexPath: method:
//add done button to navigation bar
UIBarButtonItem *doneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(userPressedDone)];
self.navigationItem.rightBarButtonItem = doneBarButtonItem;
Then have a method like this somewhere in your view controller:
-(void)userPressedDone {
//perform segue
[self performSegueWithIdentifier:#"mySegue" sender:nil];
}
I would imagine that in your -didSelectRowAtIndexPath: method you would add a UIBarButtonItem to the right or left bar button item of the navigationItem of your view controller.
I'm trying to create a custom navigation bar with title and two custom baritems.
What I've done so far:
1. subclass a UINavigationBar
2. override -(void) drawRect:(CGRect)rect method, to draw image as a background
Now I get stuck when making right bar button item.
In my CustomBar class I've overriden pushNavigationItem method as follows:
- (void)pushNavigationItem:(UINavigationItem *)item animated:(BOOL)animated {
[super pushNavigationItem:item animated:NO];
item.rightBarButtonItem.customView.frame = CGRectMake(250, 5, 60,30);
}
and in my view controller I've done something like:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationController.navigationItem.rightBarButtonItem = barButton;
[self.navigationController.navigationBar pushNavigationItem:self.navigationItem animated:NO]
but I always get SIGABRT when calling
[super pushNavigationItem:item animated:NO];
If I leave that line out, everything runs fine, but there is no button in the navigation bar.
What is the proper way to add custom bar button item to custom navigation bar?
EDIT:
For clarification, I've added an image.
This is what I need.
I'm trying to create the logout button on the right
Have you tried removingnavigationController while setting the item ? I used to do that for setting the title, never used to get the title.
Try [[self navigationItem] setRightBarButtonItem:yourButton];
In your case :
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = barButton;`
I have UItable with items. I have navigation bar etc. On one item in table "Category" I pust another UITable like this:
CategoryTableViewController *b = [[CategoryTableViewController alloc]init];
[self.navigationController pushViewController:b animated:YES];
[b release];
Now I want to add "ADD" button in navigation bar and I add UINavigationBarItem in *.xib connect it to outlets and add it like this in viewDidLoad:
self.navigationItem.rightBarButtonItem = self.addButton;
And this does not work (addButton is null), but when I put the same code for adding button in my first UITable it works fine and "ADD" button is added.
What could be the problem here?
In the ViewController that should show the button in the navigation bar type in the viewDidLoad() method:
self.addToolbarButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Add", nil)
style:UIBarButtonSystemItemAdd
target:self
action:#selector (add)] autorelease];
self.navigationItem.leftBarButtonItem = addToolbarButton;
That will add a "add" styled button to the left in the navigation bar which calls the selector method:
-(void) add {...}
in the same class when it is tapped. In this method in the same class you can specify your add logic. If this method should be placed in a different class, set the target to that.
That is the programatical way to solve this. The method "-(void) add" is what your Outlet has been in the .xib approach.
For your .xib approach you should verify that the Outlet property for the navigationBarButton is set to retain.
self.addButton is NULL, thus make sure its not NULL. Create a button from code.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneMeasuring:)];
is [super viewDidLoad]; the first call in your viewDidLoad method?
If not try to put it at the very beginning of viewDidLoad.