Click here to Skip to main content
15,868,016 members
Articles / Mobile Apps / iPhone

iPhone ComboBox

Rate me:
Please Sign up or sign in to vote.
4.88/5 (15 votes)
12 Mar 2012CPOL1 min read 120.6K   8.1K   25   25
iPhone Safari like ComboBox
iphonecombobox/iPhoneComboBox.png

Introduction

I was working on a project that required porting an Android application with many combo boxes into a native iPhone application. The requirements were to maintain the ported application look and feel; unfortunately iOS does not include a combobox control. The solution I came up with is a UITextField that uses a UIPickerView for input instead of the keyboard, similar to the Safari implementation of an HTML select field.

Implementation

I’ve created a UIViewController subclass that contains a UITextField and an arrow image to make it look like a combo box.

When a user touches the UITextField, the following action is invoked:

C++
- (IBAction)showPicker:(id)sender 
{    
    pickerView = [[UIPickerView alloc] init];
    pickerView.showsSelectionIndicator = YES;
    pickerView.dataSource = self;
    pickerView.delegate = self;

    UIToolbar* toolbar = [[UIToolbar alloc] init];
    toolbar.barStyle = UIBarStyleBlackTranslucent;
    [toolbar sizeToFit];
    
    //to make the done button aligned to the right
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc]  
    initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
       
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                   style:UIBarButtonItemStyleDone target:self
                                   action:@selector(doneClicked:)];
                                                                  
    [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];

    //custom input view
    textField.inputView = pickerView;
    textField.inputAccessoryView = toolbar;  
}

-(void)doneClicked:(id)sender
{
    [textField resignFirstResponder]; //hides the pickerView
}

Attached Source Code

  • iPhoneComboBox_src.zip contains the subclassed ControllerView for easy integration with an existing project.
  • iPhoneComboBox_demo.zip contains the source code of a demo application that uses ComboBox ControllerView.

Using the Code

  • Open Xcode and create a "Single View Application", name it ComboBoxTest.
    Make sure that "Use Automatic Reference Counting" is checked.
  • Download and unzip iPhoneComboBox_src.zip to a folder named ComboBox (double click on the zip file).
  • Drag and drop the ComboBox folder to the project in Xcode.
    Make sure that "Copy items into destination groups's folder" is checked.
    Make sure that "Create groups for any added folders" is checked.
  • Edit ViewController.h: Add #import "ComboBox.h", declare ComboBox* combo1;
    The header file should look like this:
    C++
    #import <uikit>
    #import "ComboBox.h"
    
    @interface ViewController : UIViewController
    {
        ComboBox* combo1;     
    }
    @end
  • Edit ViewController.m, add the following to viewDidLoad:
    C++
    NSMutableArray* fruitsArray = [[NSMutableArray alloc] init];
    [fruitsArray addObject:@"Apple"];
    [fruitsArray addObject:@"Banana"];
    [fruitsArray addObject:@"Orange"];
        
    combo1 = [[ComboBox alloc] init];
    [combo1 setComboData:fruitsArray];          //Assign the array to ComboBox
    [self.view addSubview:combo1.view];
    combo1.view.frame = CGRectMake(110, 69, 120, 31);      //ComboBox location and 
                            //size (x,y,width,height)
  • Build and run, that's it.

History

  • 17th December, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Dor Software
Israel Israel
Freelance Software Developer

http://dorsoft.net

Comments and Discussions

 
Generalnice idea Pin
Member 128893835-Dec-16 23:42
Member 128893835-Dec-16 23:42 
QuestionMy vote of 5... Pin
megaadam23-Oct-14 22:29
professionalmegaadam23-Oct-14 22:29 
QuestionError with the architecture Pin
MiguelSanchezCuarental27-Aug-14 14:42
MiguelSanchezCuarental27-Aug-14 14:42 
BugTestBox not working properly Pin
Member 1055327410-Jun-14 0:56
Member 1055327410-Jun-14 0:56 
GeneralMy vote is 5 Pin
IssaharNoam21-May-14 3:58
IssaharNoam21-May-14 3:58 
QuestionAbout setComboData Pin
Jargenaut21-Oct-13 8:23
Jargenaut21-Oct-13 8:23 
BugFirst element of pickerview cannot get data when click done. Pin
Jack Corleone24-Jan-13 22:17
Jack Corleone24-Jan-13 22:17 
GeneralRe: First element of pickerview cannot get data when click done. Pin
lgautier11-Feb-13 4:22
lgautier11-Feb-13 4:22 
GeneralMy vote of 5 Pin
Hatem Mostafa23-Jan-13 1:06
Hatem Mostafa23-Jan-13 1:06 
GeneralMy vote of 5 Pin
Member 977545421-Jan-13 20:07
Member 977545421-Jan-13 20:07 
QuestionTrigger event when selection is done? Pin
assilem198610-Jan-13 3:37
assilem198610-Jan-13 3:37 
QuestionLandscape picker didn't display Pin
Folus Wen16-Oct-12 23:15
Folus Wen16-Oct-12 23:15 
Generalcan't click on tabview controller in xcode 4.3 simulateor ipone 5.1 Pin
wjcroom1-Aug-12 0:34
wjcroom1-Aug-12 0:34 
QuestionCannot use this ComboBox Pin
Member 924936713-Jul-12 0:50
Member 924936713-Jul-12 0:50 
AnswerRe: Cannot use this ComboBox Pin
César D. Valverde23-Jan-14 5:48
César D. Valverde23-Jan-14 5:48 
QuestionThis doesn't work for iPad Pin
Phamer5522-May-12 12:53
Phamer5522-May-12 12:53 
Questionreloading the combo box content Pin
janwos6-Mar-12 10:40
janwos6-Mar-12 10:40 
QuestionClick not working on sub views Pin
Member 77644341-Mar-12 20:52
Member 77644341-Mar-12 20:52 
AnswerRe: Click not working on sub views Pin
BamBamBeano2-Mar-12 6:11
BamBamBeano2-Mar-12 6:11 
AnswerRe: Click not working on sub views Pin
BamBamBeano2-Mar-12 8:03
BamBamBeano2-Mar-12 8:03 
GeneralRe: Click not working on sub views Pin
Member 77644344-Mar-12 18:48
Member 77644344-Mar-12 18:48 
GeneralRe: Click not working on sub views Pin
Dor Alon12-Mar-12 1:32
Dor Alon12-Mar-12 1:32 
BugClick not working Pin
qaelbdj22-Dec-11 8:50
qaelbdj22-Dec-11 8:50 
AnswerRe: Click not working Pin
Dor Alon22-Dec-11 20:04
Dor Alon22-Dec-11 20:04 
GeneralA good Share Pin
Arif-Imran20-Dec-11 6:26
Arif-Imran20-Dec-11 6:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.