Making a dynamic NavigationBar - iphone

I want a UINavigationBar whose contents change in response to other events in the app. Most immediately, I'd like to have the buttons on the bar loaded dynamically in response to other variables in the program. But in the more general case I'd like to be able to change the contents of the UINavigationBar on the fly, while the program is running.
The hurdle I'm running into is that the UINavigationBar loads its contents when its first displayed and there doesn't seem to be a method to make it alter them after that. What's the best workaround?

This is fairly easy to do. The best way is to pre-load all of the options for the different objects you want in your navBar so that you need only switch them in and out based on the user's input, but you can load them on the fly. When the user does an action which you want to change the navBar, simply add:
self.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem * newButton = [[UIBarButtonItem alloc] initWithTitle:#"What you want to call it" style:UIBarButtonItemStyleBordered target:self action:#selector(whatYouWantTheButtonToCall)];
self.navigationItem.rightBarButtonItem = newButton;
[newButton release];
For other options then a button, you can look here. Hope that helps!

OK, I found one way:
After editing a navigation bar item, you can call
[self loadView]
...to prompt a reload.
That works, but it's a bit blech.

Related

iPhone Add Item/Row Button in Navigation Bar

Is there a standard way to implement a button like the one below in a navigation bar? If not I can, of course, just use an image or maybe a custom view but as it's used in quite a few apps I thought there may be a method I'm missing.
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem=barBtnItem;
[barBtnItem release];
this code use for creating + button.
That's one of the standard UIBarButtonSystemItem system icons (UIBarButtonSystemItemAdd) - take a look at the initWithBarButtonSystemItem:target:action: method in the UIBarButtonItem Class Reference.

iPhone/iPad: how to change location of home or cancel or any buttons in UINavigationController (with or without InterfaceBuilder)

My question is fairly straightforward and simple.
I have seen code that implements a button (such as a cancel button) that can be added to a UINavigationController. Code looks like this:
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithImage: [UIImage imageNamed:#"home.png"]
style: UIBarButtonItemStylePlain target: self
action: #selector(cancel:)];
self.navigationItem.rightBarButtonItem = cancelButton;
[cancelButton release];
What I'd like to do is to place these buttons in a custom location in the navigationItem...not in the default rightBarButtonItem or leftBarButtonItem location, but controllable perhaps using x and y co-ordinates...is this even possible? It seems like a simple request, but google searching and documentation reviewing is driving me a little nuts :)
I would appreciate any assistance with this issue.
thanks again,
Edward
I agree with 'Hack Saw' in the case of it being against the Apple 'Human Interface Guide' and thus not recommended...However... If you want to, you can draw additional views into the navigation bar's title view [self.navigationContoller.navigationBar.topItem setTitleView]. Hope that helps!
Here is link to reference: UINavigationItem
They don't make that easy because it would break user expectation. I recommend against it. If you have a serious need for it, you'll probably need to make your own navbar.

How to set the button created in the navigation bar as custom in iphone?

I am new to iphone development.I have created a UIBarButtonItem on the navigation bar.I want to set the proper to custom. There is no property as custom for "style:" attribute.Please help me out.Thanks.
leftbutton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"LEFT.png"] style:UIBarButtonItemStylePlain target:self action:#selector(leftbutton)];
self.navigationItem.leftBarButtonItem = leftbutton;
[self.navigationItem]
[leftbutton release];
If you just want to display a .png file inside the usual button frame from Apple, then your code is ok. If your png file is not displaying, just check if it's valid.
If you want to make your own button, you can use -(id)initWithCustomView:(UIView *)customView
Check this post for an interesting sample code :
Custom Bar Button Sample Code
You can create item with - (id)initWithCustomView:(UIView *)customView method with whatever custom view you want.
UIBarButtonItem has 4 initializers which cover all you will ever need:
-initWithBarButtonSystemItem:target:action: for standard (built-in items);
-initWithTitle:style:target:action: for buttons with title and no image;
-initWithImage:style:target:action: for buttons with image and no title;
-initWithCustomView: for any UIView subclass, Apple's or your own.
The style property makes sense only for standard buttons.

How to add refresh button to iPhone RSS Reader app?

I'm playing around with this application I got on last months Web Designer that's a very basic RSS reader. I would like to add a refresh button to the top navigation bar that refreshes all the content in the table but can't seem to work out how to do it. I've worked out it must somehow use the [tablename Reload] function, but have got no idea how to implement it.
I'm new to all this so simple instructions are good instructions :) I know how to add the button, its linking it to and defining the actions when the user clicks it that I'm struggling with.
You can grab the code here http://www.webdesignermag.co.uk/tutorial-files/issue-162-tutorial-files/ under iPhone Apps (it's the only one).
This is what you need to do in the RootViewController.m:
In the viewDidLoad function, add a button of type UIBarButtonSystemItemRefresh, associate to it an Action and a Target, (infact, as Alan told you, you need to learn about Outlets and Actions)
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshTable)];
self.navigationItem.rightBarButtonItem = refreshButton;
Implement refreshTable function (if not declared in .h, have to put it above viewDidLoad())
- (void)refreshTable{
[rssParser release];
rssParser = [[RSSXMLParser alloc] init];
[rssParser parseRSSFeed];
[self.tableView reloadData];
NSLog(#"table is refreshing ....");
}
Hi Graeme and velcome to SO.
For the iphone UI, you have to define outlets and actions, and use Interface Builder to link them together.
This page has some information that should hopefully get you started.
Understanding Outlets and Actions

iPhone app Action Button

I need to add an action button in my iPhone application. When that one is clicked the UIActionSheet with buttons need to be popped up.
Can any one tell me how to add an action button? - means is there any in built action button for iPhone app? or do we need to create a new button with image?
Thanks in advance!
Assuming this is in a view controller, you can do something like:
- (void)viewDidLoad
{
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(methodThatShowsSheet)];
self.navigationItem.rightBarButtonItem = actionButton;
[actionButton release];
}
(Pardon the weird indentation to make it fit in a reasonable width (I normally just let Xcode wrap-indent everything automatically)).
See Nick Veys' answer for how to implement methodThatShowsSheet.
You need to hook up the action from the button you've decided will present this action sheet to code that shows it.
See the Apple Documentation on UIActionSheet for help on doing that.
I'm not sure I understnd your question but UIKit includes the UIButton class which will automatically send a message (specified by you) to an object (also specified by you). You can create a UIButton i nInterface builder or programatically.
Without more details on whay you actually want to do or why you're finding it difficult,m it's difficult to be more precise.