UIActionSheet with dynamic entries

Sometimes you want to present some options to the user that come from a dynamic datasource.In other cases you could use an UITableView, but if you need to show a modal dialog, you can just use UIActionSheet

Here comes the sample code to build it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
UIActionSheet *viewSetMenu = [[UIActionSheet alloc]
initWithTitle: @"Select a news-page"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];

//dynamic build of buttons
//you could loop over an array
[viewSetMenu addButtonWithTitle:@"Google News"];
[viewSetMenu addButtonWithTitle:@"NTV"];
[viewSetMenu addButtonWithTitle:@"BBC"];
[viewSetMenu addButtonWithTitle:@"Cancel"];

//set index for destructive and cancel button (in the set of dynamic buttons)

[viewSetMenu setDestructiveButtonIndex:0];
[viewSetMenu setCancelButtonIndex:3];

//to avoid blocking at bottom from the tabbar,
//we should not add the sheet to a child of the tabbar

[viewSetMenu showInView:self.tabBarController.view];

thanks to Forward Echo for their hint in case you use a TabBar

Posted February 20th, 2010 in General.

Leave a response:

You must be logged in to post a comment.