ios - 当我点击 UITextField 时如何显示 UIPickerView?

标签 ios cocoa-touch uitextfield uipickerview

我从互联网上找到了很多示例代码,但也不起作用....任何人都可以告诉我下面的代码有什么问题?多谢。紧急求救

//我的storeboard屏幕
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

最佳答案

不要尝试通过滚动您自己的外观动画来模仿内置动画来重新发明轮子。将选择器 View 设置为文本字段的输入 View ,系统将为您制作动画:

textField.inputView = pickerView;

当您开始编辑文本字段时,选择器会在屏幕上为您动画显示。看我的回答here有关更多详细信息,包括在顶部添加带有“完成”按钮的工具栏。

关于ios - 当我点击 UITextField 时如何显示 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9936480/

相关文章:

ios - UITextField:如何对 "Done"键使用react并验证输入

ios - 在 Apple 的 Appstore 中为客户发布应用程序

ios - 我们可以在 WKImageView 中使用 GIF 吗?

ios - 删除 iOS 应用本地化

iphone - 关闭uiimagepickercontroller后如何使状态栏重新出现

ios - 动态调整 UITextField 大小同时在输入文本时保持其居中的正确方法是什么?

ios - 如何在 Xcode 中以编程方式获取夏令时设置?

ios - Xcode 6.2 - 'as' 之后的预期类型

objective-c - 如何纯粹用 C 编写 iOS 应用程序

swift - 光标移动到编辑格式化十进制文本字段时结束 - Swift