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

发布于 2025-01-07 12:40:19 字数 319 浏览 8 评论 0原文

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

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

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

谢谢

Can someone please provide some example code on how I could create the following features on an iOS app:

Option 1:
I want to create a text field through Interface Builder, and when someone clicks on that text field, instead of bringing up the default keyboard, I want it to bring up a Picker View which lists several choices of my liking. Once the user is done picking a certain value, they can click on a "done" button by the picker view and the picker view will go away and the Text Field will be populated with what they chose on the Picker View.

Option 2
If the previous method will require too much code to accomplish, could someone provide example code on how to create a basic Drop Down menu similar to how a standard drop down menu on a website?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2025-01-14 12:40:19

创建一个新文件。我调用了我的 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

现在,在你想要展示你的 pickerView 的类中,在 #import AEMPicker; 添加到你的 .h:

@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;
}

这应该会给你一个漂亮的可以使用的基本选择器弹出窗口。

Create a new file. I called mine AEMPicker. The .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

the .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

Now, in the class you want to present your pickerView, after #import AEMPicker; add to your .h:

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

add to your .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;
}

That should give you a pretty basic picker popover to play with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文