I have a requirement in an application where I need to be able to add otherButtonTitles dynamically, dependent upon some BOOL switches that a user has specified in the settings. However, I can't seem to figure out how to go about doing this in the UIActionSheet initialization. I've tried to pass a NSString array (NSString[2]), and also a NSArray without any luck.
Any help here is greatly appreciated.
The easiest way to do this that I have found is initially create your action sheet with no buttons, including no cancel or destructive button:
UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:#"Dynamic"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
Then add a load of buttons as needed:
if(buttonX)
{
[actionSheet addButtonWithTitle:#"Button X"];
}
if(buttonY)
{
[actionSheet addButtonWithTitle:#"Button Y"];
}
if(buttonZ)
{
[actionSheet addButtonWithTitle:#"Button Z"];
}
Then finally add the cancel button at the end and set the cancel button index:
[actionSheet addButtonWithTitle:#"Cancel"];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons - 1;
Of course you can add both a cancel button and/or a destructive button in this way.
You can add new buttons to the (already initialized) UIActionSheet with addButtonWithTitle: method. You can also create your custom UIButtons and add them to UIActionSheet's view as a subViews
I ended up solving this by using some nil strings and an array. I place the dynamic titles I need in an array, then loop through it and set the placeholder strings with as many titles as necessary. The placeholder strings are then passed to otherButtonTitles: in the action sheet initialization. Being otherButtonTitles: is terminated by nil, you can pass as many placeholder strings as necessary, as the first nil placeholder will terminate the rest.
// button titles
NSMutableArray *buttons = [[NSMutableArray alloc] init];
[buttons addObject:#"Button 1"];
[buttons addObject:#"Button 2"];
// placeholders
NSString *button0 = nil, *button1 = nil, *button2 = nil;
// put together the buttons
for (int x = 0; x < buttons.count; x++) {
switch (x) {
case 0:
button0 = [buttons objectAtIndex:x];
break;
case 1:
button1 = [buttons objectAtIndex:x];
break;
case 2:
button2 = [buttons objectAtIndex:x];
break;
}
}
// action sheet
UIActionSheet *option = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:button0, button1, button2, nil];
Hope this is helpful to others facing a similar dilemma.
If you need that many buttons, create your own modal view and your own delegate protocol.
Check the documentation for presentModalViewController:animated and dismissModalViewController:animated:
When the user dismisses your modal view, your delegate can receive a method you build, something like customActionSheetDidFinish:(int)buttonChosen
Related
I'm new to iOS and working on Google Map SDK and able to show the map on the view but now
I want to add a UITextField and enter the location over there
On buttons click that location should be shown in the map
So please kindly help me out.
I would suggest you to add buttons on the UINavigationBar or by placing a UIToolBar on the top of the map view and then by clicking on it show a UIAlertView with textfield that takes in the input and after the alert's ok is pressed take the location entered and do your stuff. Don't add buttons on the map,it would bring down the usability factor.
If you have a navigation bar already, try this
UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithTitle:#"location" style:UIBarButtonItemStyleBordered target:self action:#selector(showAlertWithTextField)];
self.navigationItem.rightBarButtonItem=locationButton;
-(void)showAlertWithTextField{
UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:#"Enter Location" message:#"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Add", nil];
[dialog setAlertViewStyle:UIAlertViewStylePlainTextInput];
[dialog show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1)
NSLog(#"%#",[[alertView textFieldAtIndex:0]text]);
}
UIActionSheet, how to dismiss it with a single click?
I have to click a button 2 times so it gets dismissed, what should I do to make it dismissble after the first click?! Here is my code:
-(void)buttonHeld:(id)sender
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:#"Delete Contact?!" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:#"Delete" otherButtonTitles: nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
[popupQuery release];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSLog(#"Delete button clicked Button Clicked");
else if (buttonIndex == 1) {
NSLog(#"Cancel Button Clicked");
}
}
Based on the name of your method "buttonHeld", my assumption is that you are trying to launch the UIActionSheet from a UILongPressGestureRecognizer. You will need to use the code listed below if you want it to work as expected. The problem stems from UILongPressGestureRecognizer being a 'continuous gesture' which fires multiple times and progresses through several different states. You'll need to monitor for the correct state and then load your UIActionSheet. I had the exact same problem and this is what solved it for me.
- (IBAction) buttonHeld:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
UIActionSheet *popupQuery = [[UIActionSheet alloc]
initWithTitle:#"Delete Contact?!"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackOpaque;
[popupQuery showInView:self.view];
}
}
This is how i dismiss the actionsheet...
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
Hope this helps
It took me a few taps to get it to cancel as well. The problem ended up being which view I attached it to.
[action showInView:self.view];
self.view was only referenced in IB. I changed it to a different view and it worked.
No need to declare in this delegate when you alloc the actionsheet you have make cancel Button that by default dismiss the ActionSheet some thing like this
[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:btnTitle,#"Send for a date range",nil];
from any other event you can use this method
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
In your code there is a syntax error you are putting else block in if block.
If u're developing for the iphone it's always safe to use :
[popupQuery showFromBarButtonItem:sender animated:YES]
or showFromTabBar: or showFromToolBar etc. instead of showinView.
I have a UIActionSheet that contains buttons that are programmatically added depending on whether a property contains a value or not.
This is the code I'm currently using:
- (IBAction)infoButtonTap:(id)sender {
MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];
if (obj != nil) {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Details"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
// Programatically add Other button titles if they exist.
if (obj.firstProperty) {
[actionSheet addButtonWithTitle:#"Dosomething"];
}
if (obj.secondProperty) {
[actionSheet addButtonWithTitle:#"Dosomethingelse"];
}
[actionSheet addButtonWithTitle:#"Static button"];
actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet showInView:self.view.window];
[actionSheet release];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
MyObject *obj = (MyObject *)[self.dataSource objectAtIndex:self.pageControl.currentPage];
switch (buttonIndex) {
case 0: // Dosomething
break;
case 1: // Dosomethingelse
break;
case 2: // Static button
break;
default:
break;
}
}
The problem is, how do I account for the fact that one of the properties may be null and handle that within the clickedButtonAtIndex:? The index values will change. I have around 4 or 5 different buttons, each displayed depending on whether a property contains a value of some sort.
The simplest way is probably to use the buttonTitleAtIndex: method of UIActionSheet to get the button title and use this to determine what action to take.
Alternatively you could use logic similar to where you add the buttons to figure out exactly what the index means.
I am implementing an action sheet. When I press "ok" button, do these, press "cancel" go back. The "ok" button is working fine, but when I press the "cancel" button, nothing happens, it doesn't retract or do anything, just hang at the actionsheet view.
Below is my code:
To create button on the nav bar:
UIBarButtonItem *clearButton = [[[UIBarButtonItem alloc] initWithTitle:#"Clear History"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(ClearHistoryAction:)] autorelease];
self.navigationItem.leftBarButtonItem = clearButton;
When I click and launch action sheet:
- (IBAction)ClearHistoryAction:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:#"Clear History?"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"OK"
otherButtonTitles:nil];
// use the same style as the nav bar
actionSheet.actionSheetStyle = self.navigationController.navigationBar.barStyle;
[actionSheet showInView:self.view];
[actionSheet release];
}
if select ok do this:
- (void)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger) buttonIndex
{
if (!buttonIndex == [actionSheet cancelButtonIndex])
{
//do what i want here!
}
}
In the header file, UIActionSheetDelegate is included in #interface.
Late but, a possible Explanation could be :
Seems to be an issue with the tabbar. If you call UIActionSheet's
[sheet showInView:self.view]
from a view controller that is a child of a UITabViewController, then
the hit testing on the cancel button fails in that portion of the
UIActionSheet that lies above the tabbar's view.
If you instead pass in the UITabBarController's view, then the UIActionSheet acts as expected.
[sheet showInView:self.parentViewController.tabBarController.view];
A more detailed explanation here:
UIActionSheet cancel button strange behaviour
I don't know if it'll solve this problem but you'll want to do buttonIndex != [actionSheet cancelButtonIndex] (check not-equal) instead of !buttonIndex == [actionSheet cancelButtonIndex] (invert buttonIndex (!) and check if that's equal).
You need to use showFromBarButtonItem instead of showInView. The problem is that the cancel button is being covered by another view--maybe a toolbar or tabbar--so it's not getting the touch events you think it should get. There are also showFromTabBar and showFromToolbar that should sometimes be used (see the UIActionSheet class reference).
I have an actionsheet popup in my iphone application. I would like to fill it with strings from an array instead of predetermined values.
I can't find anything online to do this! Perhaps actionsheet isn't the right thing to use?
Right now this is what I'm using to build it:
roomspopup = [ [ UIActionSheet alloc ]
initWithTitle: alertname
delegate: self
cancelButtonTitle: #"Cancel"
destructiveButtonTitle: nil
otherButtonTitles: #"Kitchen", "Dining Room", nil ];
But, instead of "Kitchen" and "Dining Room" I'd like it to fill in from an array. The size of the array (i.e. the number of rooms) is not a fixed number.
#JimTrell
The way to fix that would be to init the UIActionSheet without the cancel button and add this cancel button after you added your other buttons.
First init the sheet with a bunch of nil's:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Choose"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
Then loop through your array with addButtonWithTitle: and finally add the cancel button and set its index:
[actionSheet addButtonWithTitle:#"Cancel"];
[actionSheet setCancelButtonIndex:[yourArray count]];
You can't do it in one line. You'll have to call initWithTitle with an empty set of buttons, and then add your other buttons with loop using addButtonWithTitle:.
I can set up the cancel button at bottom by using this code:
anActionSheet = [[UIActionSheet alloc] initWithTitle:#"Change A/C" delegate:self
cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
for (int i = 0; i < [arraylist count]; i++)
[anActionSheet addButtonWithTitle:[arraylist objectAtIndex:i]];
anActionSheet.cancelButtonIndex = [arraylist count];
[anActionSheet addButtonWithTitle:#"Cancel"];