iPhone ComboBox

 

 

窗体顶端

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

Ive 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:

 Collapse | Copy Code

- (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:

 Collapse | Copy Code

#import <uikit>

#import "ComboBox.h"

@interface ViewController : UIViewController

{

    ComboBox* combo1;     

}

@end

· Edit ViewController.m, add the following to viewDidLoad:

 Collapse | Copy Code

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(1106912031);      //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)

About the Author

窗体底端

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值