Reputation: 16256
I try to make a UIPickerView show up when the user click on a UITextField, I should see the DataSource and the Delegate Outlets to link them with the picker, however, it doesn't exit when I open the nib file--> click on file owner--> inspector
the second problem is that the keyboard is not hidden when I click on the UItextField although I made a textFieldShouldReturn
method which suppose to hide keyboard.
What am I missing here?
the .h file:
@interface RechercherViewController : UIViewController<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate> {
IBOutlet UIPickerView *pickerTypesCarburants;
IBOutlet UIView *pickerViewTypesCarburants;
NSMutableArray *typesCarburantsArray;
IBOutlet UITextField *typeCarburantTextField;
}
-(IBAction)pickerTypeCarburantsShow;
-(IBAction)pickerTypeCarburantsDone;
@end
the .m file :
-(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;
}
Upvotes: 2
Views: 1443
Reputation: 448
First of all, you probably shouldn't be trying to make a UIPickerView appear when you tap on a UITextField as it isn't standard behaviour (especially if you're suppressing the keyboard). It sounds like you need a standard UIButton that when pressed presents the UIPickerView as this would make more sense.
Regardless, if you aren't seeing Datasource and Delegate outlets in IB, try applying them manually in your code.
pickerTypesCarburants.delegate = self;
pickerTypesCarburants.dataSource = self;
To answer your second question, the keyboard will only hide when you press the return key with the textFieldShouldReturn method you have implemented. The UITextField will also have to have its delegate set (I'm assuming you're doing this in IB as not listed in your .m file). To make the keyboard hide as soon as you press the UITextField, you would to alter your pickerTypeCarburantsShow method:
-(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];
[typeCarburantTextField resignFirstResponder];
}
This will make sure that the keyboard is hidden immediately (rather than on pressing the return button). Again, I'd question why you would want to make a UIPickerView appear when tapping a UITextField as it probably goes against the Human Interface Guidelines.
Upvotes: 2