简体   繁体   中英

How can I show a UIPickerView when I hit my UITextField?

I have found a lot of sample code from internet, but also doesn't work.... anyone can tell me what's wrong of my coding below? thanks a lot. SOS

//My storeboard screen
http://imageupload.org/en/file/209300/03.jpg.html

//This is PickerViewTest.h

@interface InputRound : UIViewController<UITextFieldDelegate>
{
    UIPickerView *pvPickerTest;
    NSMutableArray *aryMaster;
}

@property (nonatomic, retain) IBOutlet UIPickerView *pvPickerTest; 
@property (nonatomic, retain) NSMutableArray *aryMaster;

@end


//This is PickerViewTest.m

@interface InputRound ()

@end

@implementation PickerViewTest

@synthesize pvPickerTest, aryMaster;

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    aryMaster = [[NSMutableArray alloc] init];

    [aryMaster addObject:@"User01"];
    [aryMaster addObject:@"User02"];
    [aryMaster addObject:@"User03"];    
}

-(NSInteger)numberOfComponentsInPickerView:(NSInteger)component
{
    return 1;
}

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

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

-(void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    // Show UIPickerView
    pvPickerTest.frame = CGRectMake(0, 500, pvPickerTest.frame.size.width,     pvPickerTest.frame.size.height);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.50];
    [UIView setAnimationDelegate:self];

    pvPickerTest.frame = CGRectMake(0, 200, pvPickerTest.frame.size.width, pvPickerTest.frame.size.height);
    [self.view addSubview:pvPickerTest];
    [UIView commitAnimations];    
    return NO;
}

@end

Don't try and reinvent the wheel by rolling your own appearance animation to mimic the built in one. Set the picker view as the input view for your text field, and the system will do the animation for you:

textField.inputView = pickerView;

When you begin editing the text field, the picker is animated on screen for you. See my answer here for more details, including adding a toolbar on top with a "Done" button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM