2012-03-30 55 views
3

我發現了很多來自互聯網的示例代碼,但也不起作用....任何人都可以告訴我下面我的代碼有什麼問題?非常感謝。 SOS當我打開我的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 
+0

您是否將UITextFieldDelegate添加到.h和您的UITextField? – Raptor 2012-03-30 03:13:46

+0

在textFieldDidBegin中編寫代碼並給予代理 – Hiren 2012-03-30 06:18:24

回答

18

不要試圖通過滾動自己的外表動畫模仿內置的一個推倒重來。設置選擇器視圖爲您的文本字段輸入視圖,並且系統會做動畫爲您提供:

textField.inputView = pickerView; 

當你開始編輯文本字段,選取器是動畫在屏幕上你。有關更多詳細信息,請參閱我的回答here,其中包括在頂部添加一個帶有「完成」按鈕的工具欄。

+0

如何刪除動畫當它出現時 – Ujesh 2015-10-14 05:31:52

相關問題