我嘗試在用戶單擊UITextField時使UIPickerView顯示出來,我應該看到數據源和委託出口將它們與選取器相關聯,但是,它當我打開nib文件時不退出 - >點擊文件所有者 - >檢查員 第二個問題是當我點擊UItextField時鍵盤沒有隱藏,雖然我做了一個textFieldShouldReturn
方法,它假設隱藏鍵盤。UIPickerview無法在界面生成器中找到委託和數據源插座
我在這裏錯過了什麼?
.h文件:
@interface RechercherViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate> {
IBOutlet UIPickerView *pickerTypesCarburants;
IBOutlet UIView *pickerViewTypesCarburants;
NSMutableArray *typesCarburantsArray;
IBOutlet UITextField *typeCarburantTextField;
}
-(IBAction)pickerTypeCarburantsShow;
-(IBAction)pickerTypeCarburantsDone;
@end
.m文件:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [typesCarburantsArray count];
}
-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component{
return [typesCarburantsArray objectAtIndex:row];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch;
touch=[touches anyObject];
CGPoint point=[touch locationInView:self.view];
if(CGRectContainsPoint([typeCarburantTextField frame],point))
{
[self pickerTypeCarburantsShow];
}
}
-(IBAction)pickerTypeCarburantsDone{
NSInteger selectedRow=[pickerTypesCarburants selectedRowInComponent:0];
NSString *item=[typesCarburantsArray objectAtIndex:selectedRow];
typeCarburantTextField.text=[NSString stringWithFormat:@"%@",item];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 480);
pickerViewTypesCarburants.transform=transform;
[UIView commitAnimations];
}
-(IBAction)pickerTypeCarburantsShow{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGAffineTransform transform=CGAffineTransformMakeTranslation(0, 240);
pickerViewTypesCarburants.transform=transform;
[self.view addSubview:pickerViewTypesCarburants];
[UIView commitAnimations];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
嗨,thx爲您的答案,但我確實喜歡你說,沒有什麼改變:(和你的問題,我會說我的應用程序的要求迫使我這樣做 – Malloc 2011-04-16 17:28:04
嗨,你可以嘗試爲UITextField實現' - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField'委託並返回NO - 這應該防止textField變得可編輯,從而防止鍵盤出現在第一位置。或者,嘗試設置UserInteractionEnabled物業給NO。 – 2011-04-16 23:03:37