Fill an actionsheet with an array - iphone

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

Related

In UIActionSheet the cancel button not showing for iphone?

In my application when I click on change image it's asks UIActionsheet with three actions take photo ,choose from library and cancel.Take photo ,choose from library are showing but cancel didn't showing where is the problem.
this is my code
- (void)viewDidLoad
{
[super viewDidLoad];
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..."
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
}
Its because...I think you are using the default view size -Retina 4 full screen as your view size...Just try and change your view size to Retina 3.5 full screen from the Attributes inspector...!!!
You may want to take a look at the alternative methods for presenting an action sheet in the class reference. However, you may want to try using one of the following (if you have a toolbar)
– showFromBarButtonItem:animated:
– showFromToolbar:
Additionally, using the following
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
will only ever result in the action sheets style being default.
try this one...
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..."
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Cancel"
otherButtonTitles:#"Take Photo", #"Choose from library", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
Since your problem is not with your code. Maybe you are using the wrong method to display your UIActionSheet.
A UIActionSheet can be displayed with following methods:
– showFromTabBar:
– showFromToolbar:
– showInView:
– showFromBarButtonItem:animated:
– showFromRect:inView:animated:
take a look at Apple's UIActionSheet reference.
Since you use a TabBar, the Cancel button may also be below. I would test that too.
Please refer this link.
And you can refer below code also, it may help you:
(void)viewDidLoad
{
UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:#"Select Image from..." delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take Photo", #"Choose from library", nil];
[actionSheet showInView:self.view];
}

UIAlertView not showing message text

I am bringing up an UIAlertView that works fine in portrait layout, but when in landscape mode - the message doesn't appear.
It is a standard UIAlertView, with three buttons.
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"one", #"two", nil];
I have tried moving the buttons down (according to the height of the message label) and resizing the alert according to the relocated buttons, but the message still doesn't appear, despite there being plenty of room for display.
Setting the UILabel background to some color for debugging shows that it just isn't displayed..
EDIT:
The UILabel is there - It's just not being displayed.
In the willPresentAlertView method, I can see the UILabel in the NSAlertView's subviews.
It appears to be a bug with the layout code for UIAlertView. After fiddling a bit in the debugger I managed to get this workaround:
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Title" message:#"Message" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:#"one", #"two", nil];
[alert show];
// for some reason we have alpha 0 for 3 or 4 buttons
[[[alert subviews] objectAtIndex:2] setAlpha:1];
// also, for 3 buttons the height goes to 10 -> proof of concept 'fix'
[[[alert subviews] objectAtIndex:2] setFrame:CGRectMake(12, 45, 260, 24)];
[alert release];
This is just a proof of concept. A real workaroung should iterate ober the subviews and fix only labels that have either height to small or alpha==0
Probably you missed:
[alert show];
You can directly use uialertview and create object of it. Then pass title and message and button and also other button.....And call click button method.
//Example
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Title" message:#"The message."
delegate:self cancelButtonTitle:#"button 1" otherButtonTitles:#"button", nil];
[alert show];
[alert relaese];
//Then use this method
-(void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the ok/cancel buttons
if(buttonIndex==0)
{
NSLog(#"Ok");
}
else
{
NSLog(#"cancel");
}
}

UIActionSheet, how to dismiss it with a single click?

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.

How to make UIBarButtonItem give menu-options?

You know how if you click and hold on a link in Safari (for iPhone obviously) it gives you a list of options like "open in new window", "Open". "Copy" etc?
How do you call this and is it possible to get a UIBarButtonItem to do this (but every time it is clicked not just when held down)?
This is how you show this menu:
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel",
destructiveButtonTitle:nil
otherButtonTitles:#"Button 1", #"Button 2", #"Button 3", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:[self view]];
[actionSheet release];
In order to connect it to UIBarButtonItem just point the bar button item to a selector that will include the code above.
Don't forget to implement - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex method of UIActionSheetDelegate.

iPhone SDK - UIActionSheet - Dynamic Button Titles

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