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

3

我从互联网上找到了很多样例代码,但它们都不能运行……有人能告诉我下面的代码错在哪里吗?非常感谢。求救!

//我的页面设计
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

你是否在.h文件和UITextField中添加了UITextFieldDelegate? - Raptor
在textFieldDidBegin中编写你的代码,并且提供委托。 - Hiren
1个回答

18

不要试图重复造轮子来模仿内置的外观动画。将选择器视图设置为文本字段的输入视图,系统会自动为您执行动画:

textField.inputView = pickerView;

当你开始编辑文本字段时,选择器将为您在屏幕上进行动画处理。更多细节请参见我的回答此处,包括添加带有“完成”按钮的顶部工具栏。


如何在动画出现时将其移除 - Ujesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接