Home » Source Code » iPhone ComboBox

iPhone ComboBox

maninwest
2015-02-11 03:58:36
The author
View(s):
Download(s): 0
Point (s): 1 
Category Category:
MobileMobile Objective-CObjective-C

Description

Translated by maninwest@Codeforge Author:Dor Alon@CodeProject
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 aUITextFieldthat uses aUIPickerViewfor input instead of the keyboard, similar to the Safari implementation of an HTML select field.
Implementation

I’ve created aUIViewControllersubclass that contains aUITextFieldand an arrow image to make it look like a combo box.
When a user touches theUITextField, the following action is invoked:
- (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 subclassedControllerViewfor easy integration with an existing project.
iPhoneComboBox_demo.zip contains the source code of a demo application that usesComboBox ControllerView.
Using the Code


Open Xcode and create a "Single View Application", name itComboBoxTest.
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", declareComboBox* combo1;
The header file should look like this:
#import  #import "ComboBox.h" @interface ViewController : UIViewController
{
    ComboBox* combo1;     
}
@end

Edit ViewController.m, add the following toviewDidLoad:
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.

Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.96 kB
.DS_Store6.00 kB2012-03-12 06:47
01.96 kB
01.96 kB
._.DS_Store244.00 B2012-03-12 06:47
01.96 kB
.DS_Store6.00 kB2012-03-12 06:48
01.96 kB
._.DS_Store244.00 B2012-03-12 06:48
AppDelegate.h330.00 B2011-12-17 09:13
._AppDelegate.h295.00 B2011-12-17 09:13
AppDelegate.m2.36 kB2011-12-17 09:13
._AppDelegate.m295.00 B2011-12-17 09:13
01.96 kB
.DS_Store6.00 kB2011-12-17 09:14
01.96 kB
._.DS_Store244.00 B2011-12-17 09:14
arrow.png1.34 kB2011-12-17 04:12
._arrow.png244.00 B2011-12-17 04:12
ComboBox.h472.00 B2012-03-12 06:45
._ComboBox.h171.00 B2012-03-12 06:45
ComboBox.m2.62 kB2012-03-12 07:27
._ComboBox.m171.00 B2012-03-12 07:27
ComboBox.xib9.35 kB2012-03-12 07:24
._ComboBox244.00 B2012-03-12 07:27
ComboBoxTest-Info.plist1.07 kB2011-12-17 04:15
._ComboBoxTest-Info.plist244.00 B2011-12-17 04:15
ComboBoxTest-Prefix.pch327.00 B2011-12-17 04:15
._ComboBoxTest-Prefix.pch295.00 B2011-12-17 04:15
01.96 kB
.DS_Store6.00 kB2011-12-17 09:14
01.96 kB
._.DS_Store244.00 B2011-12-17 09:14
InfoPlist.strings45.00 B2011-12-17 04:15
._InfoPlist.strings295.00 B2011-12-17 04:15
ViewController.xib13.91 kB2012-03-12 07:04
._en.lproj244.00 B2012-03-12 07:04
main.m302.00 B2011-12-17 09:13
._main.m295.00 B2011-12-17 09:13
ViewController.h349.00 B2012-03-12 07:10
._ViewController.h171.00 B2012-03-12 07:10
ViewController.m3.02 kB2011-12-17 09:11
._ViewController.m295.00 B2011-12-17 09:11
._ComboBoxTest244.00 B2012-03-12 07:10
01.96 kB
project.pbxproj12.93 kB2011-12-17 04:15
01.96 kB
._project.pbxproj244.00 B2011-12-17 04:15
01.96 kB
contents.xcworkspacedata157.00 B2011-12-17 04:15
01.96 kB
._contents.xcworkspacedata244.00 B2011-12-17 04:15
01.96 kB
01.96 kB
UserInterfaceState.xcuserstate17.13 kB2012-03-12 07:28
01.96 kB
._doralon.xcuserdatad244.00 B2012-03-12 07:28
._xcuserdata244.00 B2011-12-17 04:15
._project.xcworkspace244.00 B2011-12-17 04:15
01.96 kB
01.96 kB
01.96 kB
Breakpoints.xcbkptlist91.00 B2012-03-12 07:25
01.96 kB
ComboBoxTest.xcscheme3.04 kB2011-12-17 04:15
01.96 kB
01.96 kB
01.96 kB
._ComboBoxTest.xcscheme244.00 B2011-12-17 04:15
xcschememanagement.plist484.00 B2011-12-17 04:15
._xcschememanagement.plist244.00 B2011-12-17 04:15
._xcschemes244.00 B2011-12-17 04:15
._doralon.xcuserdatad244.00 B2012-03-12 07:08
._xcuserdata244.00 B2011-12-17 04:15
._ComboBoxTest244.00 B2012-03-12 06:44
...

Comments

(Add your comment, get 0.1 Point)
Minimum:15 words, Maximum:160 words
  • 1
  • Page 1
  • Total 1

iPhone ComboBox (45.58 kB)(7.41 kB)

Need 1 Point(s)
Your Point (s)

Your Point isn't enough.

Get 22 Point immediately by PayPal

Point will be added to your account automatically after the transaction.

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more Points

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D