Xcode:使文本字段显示选取器视图或下拉菜单



有人能提供一些关于我如何在iOS应用程序上创建以下功能的示例代码吗:

选项1:我想通过界面生成器创建一个文本字段,当有人点击该文本字段时,我希望它不会显示默认键盘,而是显示一个Picker View,其中列出了我喜欢的几个选项。一旦用户完成了某个值的选取,他们就可以点击选取器视图旁边的"完成"按钮,选取器视图将消失,文本字段将填充他们在选取器视图上选择的内容。

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

感谢

创建一个新文件。我叫我的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;
}

这应该会给你一个非常基本的选择弹出式游戏。

最新更新