Add UIBarButtonItem in interface builder, how to? - iphone

I have a navigation based application that have a uinavigataioncontroller containing uitableviewcontrollers. I want to add buttons to the UINavigationbar of the uinavigataioncontroller, usually I write code to add these buttons, something like this:
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(saveClicked:)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
My idea now was to use interface builder instead. But I'm not sure how to do it. What I'm trying to do is to add a UINavigationItem in the xib file of the viewcontroller (my viewcontroller is called TextViewController), something like this:
See this image: http://i48.tinypic.com/qq5yk7.jpg
But how can I make TextViewController use the UINavigationItem I added? The button I add doesn't show in the navigationbar.
Any ideas on how to do this? What am I missing?

You can.
All you have to do is drag UIBarButtonItem from Object Library to corresponding NavigationBar of your Xib.
After that then you can connect it to become an IBOutlet of your class.

You can take a bar button in the coding like IBOutlet UIBarButtonItem * saveButton;
Then you have to take a bar button in the xib file and bind this button with that and in coding you can directly write
self.navigationItem.rightBarButtonItem = saveButton;
Its almost the same as your code but the only thing changes is you dont have to initialize the barbutton..
Happy coding...

Related

rightbarbutton not animating properly

I am animating to a view with it sliding from right to left. The left bar button looks like it is in the navigation but the rightbarbutton seems to animated weirdly, almost like at first its not part of the navigation bar.
I am wanting to know if there is something wrong in my code... or if there is another way of doing this?
- (void)viewWillAppear:(BOOL)animated
{
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Skip" style:UIBarButtonItemStyleBordered target:nil action:nil];
[super viewWillAppear:animated];
}
Any help would be greatly appreciated.
Set rightBarButtonItem in the view controller's init... method. It only has to be done once in the view controller's lifetime.
This is just a shot in the dark, but the convention is to first call [super viewWillAppear:animated], then add your own code. Do that whenever you're constructing things (objects, views, etc).
Then when tearing things down (-dealloc, -viewWillDisappear, etc.), do the opposite. Clean up your stuff, then call the super to let it do its cleanup.
That probably isn't related and won't make a difference. But it's a good habit to get in to, and you never know. It might change the behavior here. Maybe the super needs to do do some setup before you add the bar button item.

How can I hide a UIBarButtonItem?

I have created a simple UI in IB, this consists of a UINavigationBar and a UIBarButtonItem that I dragged and dropped on the right hand side.
I am trying to set this button to be hidden a certain times but I am having some problems.
So far I have tried using:
self.NavigationItem.rightBarButton = nil;
...which didn't work for me. I have also tried creating and IBOutlet and linking it to the button however I'm having problems with this too. I think it should be pretty simple and maybe I'm over-complicating it, but at this point I'm pretty stumped!
Please can someone help me out?
UINavigationItem doesnt have a rightBarButton property. Try rightBarButtonItem instead (or [self.navigationItem setRightBarButtonItem:nil animated:NO];):
self.navigationController.navigationItem.rightBarButtonItem = nil;
// Or
self.navigationItem.rightBarButtonItem = nil;
// Or
[self.navigationItem setRightBarButtonItem:nil animated:NO];
Just reset the buttons
-(void)setItems:(NSArray *)items animated:(BOOL)animated
More info here: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instm/UIToolbar/setItems%3aanimated%3a
You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.
You can also add a UIButton as the UIBarButtonItem's customView. Then set the hidden property on the customView (UIButton)
Rather than deleting the bar button item and destroying the button and it's attached storyboard segue, you can just set it to clear text when it's disabled.
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:#{NSForegroundColorAttributeName:[UIColor clearColor]}
forState:UIControlStateDisabled];
Then when ever you want the bar button item hidden, you can just do:
self.navigationItem.rightBarButton.enabled = NO;
It's lame there's no hidden property but this offers the same result.
Actually, you can just create an IBOutlet reference to the desired UIBarButtonItem and when needed just do as follow:
[self.yourOutletRerence setImage: nil];
The simplest solution: Just change the BarButtonItem's identifier to custom.

What's the best way to implement a Back button on the Navigation Bar of an iPhone app

Fairly new to the coding community and took over an app from my developer. I want to implement a Back button on the navigation bar of one of the views in my app. If you guys could point me in the right direction, that'd be greatly appreciated.
Thanks very much!
in some scenarios, where you are not using an UINavigationController, you can create a UINavigationBar yourself.
self.navBar = [UINavigationBar new];
self.navBar.contentMode = UIViewContentModeTopLeft;
self.navBar.delegate = self;
self.navBar.frame = CGRectMake(0, 30, mainWindow.frame.size.width,40);
UINavigationItem *backButton = [[UINavigationItem alloc] initWithTitle:#"Back"];
[self.navBar setItems:[NSArray arrayWithObjects:backButton,[[UINavigationItem alloc] initWithTitle:#"Current View"], nil]];
[self.view addSubview: self.navBar];
Your controller should then implement the UINavigationBarDelegate protocol, so that you can respond to
-(BOOL) navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
where you can go back to your previous view.
There is mostly no use-case for this. You'll probably really want to work with UINavigationControllers and the push- and popViewController methods.
I hope this helped anyway.
kind regards.
The back button is added automatically when you use [UINavigationController -pushViewController]. Basically, instead of having a regular view controller, you need a navigation controller that is initialized with the default view, and when you want to show the other view, you need to push it on the navigation controller. By pushing the new view controller, it automatically adds the back button. The title of the back button is typically the title of the view controller, but you can change it with navigationBar.backItem.title.

add searchbar in UItoolbar

i want to know is this possible to add a searchbar in UItoolbar
I just had a quick look and while interface builder will let you do it, when you try to save a warning pops up a warning saying the following "UISearchBars are not supported in UIBarButtonItems in non-iPad documents". That being said, it might be possible to make your own view that looks and works like a UIToolbar that does allow a UISearchBar.
You can nest:
navItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView: m_searchBar] autorelease];
If you get an error stating:
UISearchBars are not supported in UIBarButtonItems in non-iPad documents
adding a UISearchBar to a view within a UIBarButtonItem worked for me.

Access UI elements created in Interface Builder in the controller?

This is a very simple iPhone / Cocoa question. I have a button that transitions between two views. I set most of this up using interface builder. When I click the button and the second view is displayed, how do I programmatically change the text of the button (to say 'back', for instance)?
In your class declaration, declare a button object, and make sure it is specified as an IBOutlet:
IBOutlet UIButton* myButton;
Once you save this change, if you go back to Interface Builder, you should see this outlet when you right click on your File's Owner (assuming you have specified the File's Owner properly). Associate this outlet with the onscreen UIButton by right-click dragging.
Now the on screen object is associated with your in-code name.
Whereever you want to change the text on the UIButton, just say:
[myButton setTitle:#"Back - or whatever else you want it to say"
forState: UIControlStateNormal ];
// you can set different title text for each state
// of the button (selected, active, or normal)
Alternately, you can assign a unique tag to the control and use -[UIView viewWithTag:] to get a UIView pointer to the control. In most cases, outlets are the preferred mechanism, but tags are useful in things like table cells.
You'll need to set up an outlet for IB:
#interface MyViewController : UIViewController {
IBOutlet UIButton *myButton;
}
Save, bring up IB, set the file's owner to MyViewController, then create referencing outlets from both your UIView and UIButton to the appropriate points in the file's owner in the Connections Inspector.
Then in the implementation, you could do:
[myButton setTitle:#"Back" forState:UIControlStateNormal];
If you get lost with the connections, I'd recommend having a look at the lecture notes and video lectures at http://www.stanford.edu/class/cs193p/cgi-bin/index.php , which go into some detail about iPhone basics.
Hope that helps.
Most of the posts so far have focused on creating the button, and are very correct. The following answers the rest of the question:
When I click the button and the second view is displayed, how do I programmatically change the text of the button (to say 'back', for instance)?
The easiest way, if I understand your circumstance correctly, is to use the plug-n-play UINavigationBarController. First you want to push your second view controller onto the view stack:
// In firstViewController.m
self.navigationController = [[UIViewController alloc]initWithNibName:#"secondView" bundle:nil];
[self pushViewController:secondViewController animated:TRUE];
When your second view controller is shown, you should automatically get a back button on the left side of the nav-bar. If there is a need to change the text of that back button, you can simply refer to it like so:
// In secondViewController.m
-(void)ViewWillAppear
{
[self.navigationItem.backBarButtonItem setText:#"GoBack"];
}
There are also the left and rightBarButtonItem(s) which are handy for more complex navigation. Here is a less plug-n-play scenario:
// In secondViewController.h
-(IBAction)goBack; (this should appear as an action in your associated nib file)
// In secondViewController.m
-(void)viewDidLoad
{
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"GoBack" style:UIBarButtonItemStylePlain target:self action:#selector(goBack)];
}
-(IBAction)goBack
{
// logic to be done before going back
[self popViewControllerAnimated:secondViewController animated:TRUE];
}
You need to define and connect an "outlet". Take a look at the documentation, making note of the section on "Connections and Bindings".