tags:

views:

354

answers:

4

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.

+1  A: 

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:.

Adam Rosenfield
A: 

If you do that, however, you end up with the Cancel button at the top of the list.

Is there a way to fix that?

Actually, I couldn't figure out how to get around that. So I did what I should have done in the first place, which is to make a tableview + show it. Another problem is that I wanted the list to remember where it was, which a tableview does automatically.
cmos
+2  A: 

@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]];
Dirk van Oosterbosch
A: 

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

Saw Sandar
anActionSheet.cancelButtonIndex=[arraylist count]; is the main point.U will hv to declare it after looping so that cancel button is set up after all other buttons.
Saw Sandar