[iPhone中级] IOS中实现自定义UICombox
我们在做IOS开发的时候,有时候会限制于系统自带的一些控件,而无法做到更好的用户体验,今天我们就来介绍一下我们自己做的UICombox控件,先来看一下图:
这是我们自定义的控件,实现了点击输入框,弹出数据拾取器的效果
首先我们先来整理一下思路,UICombox看上去像UITextField吧,只是旁边多了一个小图片,那我们就可以通过继承UITextField来实现,并重新整理UITextField的框架。
接下来就是下面的数据拾取器了,看到半遮照的效果,我们应该能想到是UIActionSheet吧,只不过我们把Action中的按钮换成了我们自定义的效果,好了,思路整理得差不多,我们就来编码了
[java]
<SPAN style="FONT-SIZE: 18px">#import <Foundation/Foundation.h>
@inte易做图ce UICombox: UITextField<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {
@private
UIActionSheet *action;
UIPickerView *picker;
}
@property(nonatomic, copy) NSArray *items;
- (void)initComponents;
@end</SPAN>
#import <Foundation/Foundation.h>
@inte易做图ce UICombox: UITextField<UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {
@private
UIActionSheet *action;
UIPickerView *picker;
}
@property(nonatomic, copy) NSArray *items;
- (void)initComponents;
@end这里我说一下,首先我们的UICombox继承了UITextField,接着需要实现UIPickerView的一些方法才能产生我们需要的效果。items是由我们前部传过来UICombx中需要显示的值。还定义了一个初始化组件的方法。
[java]
<SPAN style="FONT-SIZE: 18px">-(void) didMoveToWindow {
UIWindow* appWindow = [self window];
if (appWindow != nil) {
[self initComponents];
}
}</SPAN>
-(void) didMoveToWindow {
UIWindow* appWindow = [self window];
if (appWindow != nil) {
[self initComponents];
}
}初如化组件,程序启动的时候就进行初始化
[java] view plaincopyprint?<SPAN style="FONT-SIZE: 18px">- (void)initComponents{
if(action != nil) return;
//Create UIDatePicker with UIToolbar.
action = [[UIActionSheet alloc] initWithTitle:@""
delegate:nil
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
//创建PickView
picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
picker.showsSelectionIndicator = YES;
picker.delegate = self;
picker.dataSource = self;
//顶部工具条
UIToolbar *datePickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
datePickerToolbar.barStyle = UIBarStyleBlackOpaque;
[datePickerToolbar sizeToFit];
//定义两个按钮
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *btnFlexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
[barItems addObject:btnFlexibleSpace];
[btnFlexibleSpace release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(doCancelClick:)];
[barItems addObject:btnCancel];
[btnCancel release];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(doDoneClick:)];
[barItems addObject:btnDone];
[btnDone release];
[datePickerToolbar setItems:barItems animated:YES];
[barItems release];
[action addSubview: datePickerToolbar];
[action addSubview: picker];
[datePickerToolbar release];
}
</SPAN>
- (void)initComponents{
补充:移动开发 , IOS ,