Xcode:使文本字段打开拾取器视图或下拉菜单

时间:2012-02-20 16:26:12

标签: xcode4 ios5 uitextview uipickerview drop-down-menu

有人可以提供一些示例代码,说明如何在iOS应用上创建以下功能:

选项1: 我想通过Interface Builder创建一个文本字段,当有人点击该文本字段时,我不想打开默认键盘,而是想要显示一个Picker View,它列出了我喜欢的几个选项。一旦用户完成选择某个值,他们就可以通过选择器视图点击“完成”按钮,选择器视图将消失,文本字段将填充他们在选择器视图中选择的内容。

选项2 如果以前的方法需要太多的代码来完成,有人可以提供有关如何创建基本下拉菜单的示例代码,类似于网站上的标准下拉菜单吗?

由于

1 个答案:

答案 0 :(得分:1)

创建一个新文件。我打电话给我的AEMPicker。 .h:

@protocol AEMPickerDelegate <NSObject>

-(void)touchedPicker:(NSString *)string;

@optional
-(void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k;

@end

@interface AEMPicker : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
    UIPickerView *pickerView;
}
@property (nonatomic, strong) NSArray *contentArray;
@property (nonatomic, assign) id<AEMPickerDelegate> delegatePicker;

- (id)initWithArray:(NSArray *)contents inFrame:(CGRect)pickerFrame;
@end

.m:

#import "AEMPicker.h"

@implementation AEMPicker

@synthesize contentArray;
@synthesize delegatePicker;

- (id)initWithArray:(NSArray *)contents inFrame:(CGRect)pickerFrame
{
    self = [super init];
    if (self) {
        contentArray = [NSArray arrayWithArray: contents];

        pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
        pickerView.showsSelectionIndicator = YES;
        pickerView.delegate = self;

        [self.view addSubview:pickerView];
    }
    return self;
}
-(void)setInitialPickerValueToRow:(int)i inComponent:(int)j animated:(BOOL)k{
    [pickerView selectRow:i inComponent:j animated:k];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [contentArray count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [contentArray objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    [self.delegatePicker touchedPicker:[contentArray objectAtIndex:row]];
}

@end

现在,在#import AEMPicker;添加到.h后,您想要在班级中展示您的pickerView:

@interface YourClass : UIViewController <AEMPickerDelegate, UITextFieldDelegate>{
AEMPicker *picker;
UIPopoverController *pickerPopOver;
UIPopoverController *pOC;
CGRect popRect;
}

添加到.m:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [textField resignFirstResponder];

    popRect = CGRectMake(406, 110, 0, 0);
    CGRect pickerRect = CGRectMake(0, 10, 0, 0);

    NSArray *contents = [[NSArray alloc] initWithObjects:@"Object 1", @"Object 2", @"Object 3", nil];
    picker = [[AEMPicker alloc] initWithArray:contents inFrame:pickerRect];
    picker.delegatePicker = self;

    pickerPopOver = [[UIPopoverController alloc] initWithContentViewController:picker];
    pickerPopOver.popoverContentSize = CGSizeMake(320, 250);
    [pickerPopOver presentPopoverFromRect:popRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:TRUE];

        pOC = pickerPopOver;
}

-(void)touchedPicker:(NSString *)string{

    [yourTextField setText:string];
    [pickerPopOver dismissPopoverAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    [pOC dismissPopoverAnimated:NO];
    return YES;
}

这应该会给你一个非常基本的选择器弹出窗口。