Adding a button to a cell - iphone

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.

Related

How to make UINavigationItem backBarButtonItem rectangular?

How can set self.navigationItem.backBarButtonItem of my RootViewController, so that the back button is rectangular instead of having a back arrow? I want to do this because I'm using a custom backBarButtonItem with an image of a grid of four squares (like the nine-square-gird image that the Facebook iPhone app uses for its home button).
Currently, in -[RootViewController initWitNibName:bundle:], I do:
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"go-home.png"]
style:UIBarButtonItemStylePlain
target:nil action:NULL];
Note: This does not cause a memory leak as I'm using ARC.
But, this makes the button have a left arrow. Is there a simple fix to make the button rectangular on all sides?
I know I could set the leftBarButtonItem for all of the view controllers that can get pushed from the RootViewController, but there are like five different options, so that'd be a lot of repetition. I guess I could make a method, e.g., +[Utils homeBarButtonItem], that creates the button above and then call self.navigationItem.leftBarButtonItem = [Utils homeBarButtonItem]; in each of the five view controllers' -viewDidLoad methods, but I'm wondering if there's a simple fix I'm missing.
Sadly the only way, as you suggest, is to use a leftBarButtonItem and use a button builder utility class.
Set the action of your leftBarButtonItem to pop the view controller and you're done.
[self.navigationController popViewControllerAnimated:YES];

how to add items to the UINavigationBar

I am new to iPhone development.
my application is based on UInavigationBar .
I want to add a navigation bar item in one of my xib, but in the .xib i just simulate the navigation bar so i can't drag and drop the item. thank you
You'll want to add the nav bar buttons programmatically. You see, your xib has a view that is shown within the content view of the UINavigationController. It is the UINavigationBar to which your app has access and which controls the nav bar items. As you point out, your xib has just a placeholder for the nav bar, which is really a convenience for you so your view is sized correctly as you lay it out.
In your UIViewController for the xib, you an add view-appropriate nav bar items with code something like
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"View" style:UIBarButtonItemStylePlain
target:self
action:#selector(viewControls:)] autorelease];
Does that make sense?
In order to be able to add items into a UINavigationBar, you need to first add a UINavigationBar to your view and then add items to it.
You cannot drag and drop items on a simulated navigation bar. A simulated Navigation Bar is just there to make sure you have a correct estimate of the view size available to you if you are adding a Navigation Bar by some other means or from code.
You should be using a UINavigationController for a navigation based hierarchy. That will take care of a lot of the lower details of how to make navigation work as you would like it to. I would also recommend setting that all up programmatically. Here is how you would do that.
// Initial setup of navigation
YourViewController *yvc = [[YourViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yvc];
[[self view] addSubview:[nav view]];
Then when you want to go to a new view controller (the animated sliding that you normally see), you do this
// From inside 'YourViewController',
// this is normally when the user touches a table view cell
NewViewController *nvc = [[NewViewController alloc] init];
[self.navigationController pushViewController:nvc];
If you want to change the title or the buttons, you do this
// This is normally in viewDidLoad or something similar
[self.navigationItem setTitle:#"Hello World!"];
[self.navigationItem.rightBarButtonItem:/* A UIBarButtonItem */];

Disabling back button and put Cancel button in navigation view controller -- like contacts iPhone application

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.

UINavigationController Toolbar Buttons

I have a UINavigationController that I've set as the rootViewController of my window. In the NIB file I've set it up so that it has a "Bottom Bar" of "Toolbar". In Interface Builder I've added a UIBarButtonItem. This all works great and I can handle the button click fine. When I hit one of the buttons, I push a new view onto the ViewController and that works fine too. One problem, my button disappears when the view is loaded. Now in the subsequent view I can set the bottom bar to be a Toolbar and I see it in Interface Builder, but I cannot add any buttons to it.
I know I'm either missing something obvious or thinking about this incorrectly but how do I add UIBarButtonItems to subsequent views pushed to my nav controller? I'm talking the bar at the bottom, not the nav bar at the top.
Any ideas?
The toolbarItems property on the UIViewController is what you are interested in. You can create UIBarButtonItems programmatically and add them to a new toolBarItems array in viewDidLoad.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* editButton = [[[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editAction)] autorelease];
[self setToolbarItems:[NSArray arrayWithObject:editButton]];
}
This worked better for me:
[[self navigationItem] setLeftBarButtonItem:homeButton];
You can do the same for the right side.

What happens when back button is pressed in navigationBar

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];
}