Xcode: Make A Text Field Bring Up A Picker View or

2019-04-14 12:26发布

问题:

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

回答1:

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.