iPhone ComboBox
|
|
||||||||||||||||
DescriptionTranslated 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 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 |
---|---|---|
0 | 1.96 kB | |
.DS_Store | 6.00 kB | 2012-03-12 06:47 |
0 | 1.96 kB | |
0 | 1.96 kB | |
._.DS_Store | 244.00 B | 2012-03-12 06:47 |
0 | 1.96 kB | |
.DS_Store | 6.00 kB | 2012-03-12 06:48 |
0 | 1.96 kB | |
._.DS_Store | 244.00 B | 2012-03-12 06:48 |
AppDelegate.h | 330.00 B | 2011-12-17 09:13 |
._AppDelegate.h | 295.00 B | 2011-12-17 09:13 |
AppDelegate.m | 2.36 kB | 2011-12-17 09:13 |
._AppDelegate.m | 295.00 B | 2011-12-17 09:13 |
0 | 1.96 kB | |
.DS_Store | 6.00 kB | 2011-12-17 09:14 |
0 | 1.96 kB | |
._.DS_Store | 244.00 B | 2011-12-17 09:14 |
arrow.png | 1.34 kB | 2011-12-17 04:12 |
._arrow.png | 244.00 B | 2011-12-17 04:12 |
ComboBox.h | 472.00 B | 2012-03-12 06:45 |
._ComboBox.h | 171.00 B | 2012-03-12 06:45 |
ComboBox.m | 2.62 kB | 2012-03-12 07:27 |
._ComboBox.m | 171.00 B | 2012-03-12 07:27 |
ComboBox.xib | 9.35 kB | 2012-03-12 07:24 |
._ComboBox | 244.00 B | 2012-03-12 07:27 |
ComboBoxTest-Info.plist | 1.07 kB | 2011-12-17 04:15 |
._ComboBoxTest-Info.plist | 244.00 B | 2011-12-17 04:15 |
ComboBoxTest-Prefix.pch | 327.00 B | 2011-12-17 04:15 |
._ComboBoxTest-Prefix.pch | 295.00 B | 2011-12-17 04:15 |
0 | 1.96 kB | |
.DS_Store | 6.00 kB | 2011-12-17 09:14 |
0 | 1.96 kB | |
._.DS_Store | 244.00 B | 2011-12-17 09:14 |
InfoPlist.strings | 45.00 B | 2011-12-17 04:15 |
._InfoPlist.strings | 295.00 B | 2011-12-17 04:15 |
ViewController.xib | 13.91 kB | 2012-03-12 07:04 |
._en.lproj | 244.00 B | 2012-03-12 07:04 |
main.m | 302.00 B | 2011-12-17 09:13 |
._main.m | 295.00 B | 2011-12-17 09:13 |
ViewController.h | 349.00 B | 2012-03-12 07:10 |
._ViewController.h | 171.00 B | 2012-03-12 07:10 |
ViewController.m | 3.02 kB | 2011-12-17 09:11 |
._ViewController.m | 295.00 B | 2011-12-17 09:11 |
._ComboBoxTest | 244.00 B | 2012-03-12 07:10 |
0 | 1.96 kB | |
project.pbxproj | 12.93 kB | 2011-12-17 04:15 |
0 | 1.96 kB | |
._project.pbxproj | 244.00 B | 2011-12-17 04:15 |
0 | 1.96 kB | |
contents.xcworkspacedata | 157.00 B | 2011-12-17 04:15 |
0 | 1.96 kB | |
._contents.xcworkspacedata | 244.00 B | 2011-12-17 04:15 |
0 | 1.96 kB | |
0 | 1.96 kB | |
UserInterfaceState.xcuserstate | 17.13 kB | 2012-03-12 07:28 |
0 | 1.96 kB | |
._doralon.xcuserdatad | 244.00 B | 2012-03-12 07:28 |
._xcuserdata | 244.00 B | 2011-12-17 04:15 |
._project.xcworkspace | 244.00 B | 2011-12-17 04:15 |
0 | 1.96 kB | |
0 | 1.96 kB | |
0 | 1.96 kB | |
Breakpoints.xcbkptlist | 91.00 B | 2012-03-12 07:25 |
0 | 1.96 kB | |
ComboBoxTest.xcscheme | 3.04 kB | 2011-12-17 04:15 |
0 | 1.96 kB | |
0 | 1.96 kB | |
0 | 1.96 kB | |
._ComboBoxTest.xcscheme | 244.00 B | 2011-12-17 04:15 |
xcschememanagement.plist | 484.00 B | 2011-12-17 04:15 |
._xcschememanagement.plist | 244.00 B | 2011-12-17 04:15 |
._xcschemes | 244.00 B | 2011-12-17 04:15 |
._doralon.xcuserdatad | 244.00 B | 2012-03-12 07:08 |
._xcuserdata | 244.00 B | 2011-12-17 04:15 |
._ComboBoxTest | 244.00 B | 2012-03-12 06:44 |
Sponsored links
Comments
(Add your comment, get 0.1 Point)
Minimum:15 words, Maximum:160 words
- 1
- Page 1
- Total 1