Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill an actionsheet with an array

Tags:

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.

like image 784
cmos Avatar asked Mar 19 '09 21:03

cmos


2 Answers

@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]];
like image 158
Dirk van Oosterbosch Avatar answered Sep 28 '22 18:09

Dirk van Oosterbosch


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

like image 32
Adam Rosenfield Avatar answered Sep 28 '22 20:09

Adam Rosenfield