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.
Related
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.
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.
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 can I add to my app that little "plus" button that the Contacts application has in the upper right hand corner of the window?
I would like to be able to hit that button and bring up a screen to add a new entry to the table view.
What you're talking about is a UIBarButtonItem.
Here's an example of how to set one up in code:
UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showContactView:)];
[self.navigationItem setRightBarButtonItem:addButton];
[addButton release];
When tapped, the button will call the follow selector.
- (void)showContactView:(id)sender {
// Show new contact view.
}
Tom Irving is completely right. It is a UIBarButton and you can configure as he has said. It can be added to a UINavigationBar or UIToolbar as per your requirement.
Hope this helps.
Also dont forget to release this button because some times it causes a crash if you dont release the button instance.
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