Breaking News
Home / iOS / iOS Snippets / How do you create UIActionSheet elements dynamically in iOS?

How do you create UIActionSheet elements dynamically in iOS?

Dyamic UIActionSheet iOS TutorialUIActionSheet provides a simple way to present a list of choices to the user. The user will select one of the choices presented. For each of these choices an action can be assigned on click of the choice. Normally choice elements in the UIActionSheet are fixed/defined at the time of creation using the constructors as.


UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle: @”Cancel” destructiveButtonTitle:nil otherButtonTitles: @”Button A”,@”Button B”,@”Button C”,nil, nil];

Alternatively we want to define menu items conditionally so we need a way to create it dynamically on fly. The following shows how to do so:

Create an action sheet with no menu items.

UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@”Choose”

Then conditionally add button menu items as needed:

    [actionSheet addButtonWithTitle:@”Button A”];

  if (…)

    [actionSheet addButtonWithTitle:@”Button B”];

    [actionSheet addButtonWithTitle:@”Button C”];

Then finally add the Cancel button at the end and set the cancel button index:

[actionSheet addButtonWithTitle:@”Cancel”];
actionSheet.cancelButtonIndex = actionSheet.numberOfButtons – 1;

In the similar way you can add the destructive button.


Check Also

EKCalendar – That account does not allow calendars to be added or removed

Usecase When I am trying to create a new Calendar programmatically using the EventKit API …

Advertisment ad adsense adlogger