[英]How can I show a UIPickerView when I hit my UITextField?
我從互聯網上找到了很多示例代碼,但也沒有用....任何人都可以告訴我下面編碼的錯誤是什么? 非常感謝。 SOS
//我的櫥櫃屏幕
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
不要試圖通過滾動自己的外觀動畫來模仿內置的動畫來重新發明輪子。 將選擇器視圖設置為文本字段的輸入視圖,系統將為您執行動畫:
textField.inputView = pickerView;
當您開始編輯文本字段時,選擇器會在屏幕上為您設置動畫。 見我的答案在這里了解更多詳情,包括頂部添加工具欄與“完成”按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.