Search code examples
iosiphoneuipickerviewactionsheetpicker

Add UIPickerView & a Button in Action sheet - How?


My application requires following things to be added in an action sheet.

  • UIToolbar
  • Button on UIToolbar
  • UIPicker Control

I have included an image to understand my requirements.

alt text

Could you please explain, how this can be implemented?


Solution

  • Update for iOS 7

    Apple docs for UIActionSheet: UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy

    I recommend against trying to customize the contents of an ActionSheet, as it can lead to serious invalid context errors in iOS 7. I just spent a few hours working through this problem and ultimately decided to take a different approach. I replaced the call to show the action sheet with a modal view controller containing a simple tableview.

    There are many ways to accomplish this. Here's one way that I just implemented in a current project. It's nice because I can reuse it between 5 or 6 different screens where I all users to select from a list of options.

    1. Create a new UITableViewController subclass, SimpleTableViewController.
    2. Create a UITableViewController in your storyboard (embedded in a navigation controller) and set its custom class to SimpleTableViewController.
    3. Give the navigation controller for SimpleTableViewController a Storyboard ID of "SimpleTableVC".
    4. In SimpleTableViewController.h, create an NSArray property that will represent the data in the table.
    5. Also in SimpleTableViewController.h, create a protocol SimpleTableViewControllerDelegate with a required method itemSelectedatRow:, and a weak property called delegate of type id<SimpleTableViewControllerDelegate>. This is how we will pass the selection back to the parent controller.
    6. In SimpleTableViewController.m, implement the tableview data source and delegate methods, calling itemSelectedatRow: in tableView:didSelectRowAtIndexPath:.

    This approach has the added benefit of being fairly reusable. To use, import the SimpleTableViewController class in your ViewController.h, conform to the SimpleTableViewDelegate, and implement the itemSelectedAtRow: method. Then, to open the modal just instantiate a new SimpleTableViewController, set the table data and delegate, and present it.

    UINavigationController *navigationController = (UINavigationController *)[self.storyboard instantiateViewControllerWithIdentifier:@"SimpleTableVC"];
    SimpleTableViewController *tableViewController = (SimpleTableViewController *)[[navigationController viewControllers] objectAtIndex:0];
    tableViewController.tableData = self.statesArray;
    tableViewController.navigationItem.title = @"States";
    tableViewController.delegate = self;
    [self presentViewController:navigationController animated:YES completion:nil];
    

    I create a simple example and posted it on github.

    Also see Showing actionsheet causes CGContext invalid context errors.