如何在选择多个文本字段时添加选择器,我已经有一个文本字段正在工作?
我已经在我的 Nib 文件上连接了三个 TextField 出口
IBOutlet UITextField *myTextField;
IBOutlet UITextField *myTextFieldd;
IBOutlet UITextField *myTextFields;
,现在我正在尝试添加一个选取器以在您选择 myTextFieldd 和 myTextFields 时弹出,请注意 myTextField 效果很好。我还尝试使用相同的选择器来弹出三个文本字段。
代码:
#import“PickrAppViewController.h”
@implementation PickrAppViewController
@synthesize categoryArray,selectedCategory;
- (void)viewDidLoad {
[super viewDidLoad];
categoryArray = [[NSMutableArray alloc] initWithObjects:@"Jack",@"Jone",nil];
}
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == myTextField)
{
[numberTextField resignFirstResponder];
if([myTextField.text isEqualToString:@""]){
myTextField.text = [self.categoryArray objectAtIndex:0];
}
NSInteger pickerRow;
for(NSInteger i = 0; i < [self.categoryArray count]; i++){
NSString *string = [self.categoryArray objectAtIndex:i];
if([string isEqualToString:myTextField.text]){
pickerRow = i;
break; //Once we have it break out of the loop
}
}
[picker selectRow:pickerRow inComponent:0 animated:NO];
pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.50];
[UIView setAnimationDelegate:self];
pickrView.frame = CGRectMake(0, 300, pickrView.frame.size.width, pickrView.frame.size.height);
[self.view addSubview:pickrView];
[UIView commitAnimations];
return NO;
}
if([pickrView superview]){
[self animationForPickrDown];
}
return YES;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([pickrView superview]){
[self animationForPickrDown];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark
#pragma mark PickrView datasource methods
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.selectedCategory = [NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:row]];
myTextField.text = self.selectedCategory;
[self animationForPickrDown];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.categoryArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.categoryArray objectAtIndex:row];
}
- (void) animationForPickrDown
{
[UIView beginAnimations:nil context:NULL];
pickrView.frame = CGRectMake(0, 300, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView setAnimationDuration:.50];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stopAnimation)];
pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView commitAnimations];
}
- (void) stopAnimation
{
if([pickrView superview]){
[pickrView removeFromSuperview];
}
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[myTextField release];
[pickrView release];
[selectedCategory release];
}
@end
谢谢
I Already have three TextField Outlet connected on my Nib File
IBOutlet UITextField *myTextField;
IBOutlet UITextField *myTextFieldd;
IBOutlet UITextField *myTextFields;
Now I am Trying To Add a Picker to Pop Out when you select myTextFieldd and myTextFields, notice that myTextField Works Great. I Also am Trying to Use the Same Picker to POP Up for the Three TextFields.
Code:
#import "PickrAppViewController.h"
@implementation PickrAppViewController
@synthesize categoryArray,selectedCategory;
- (void)viewDidLoad {
[super viewDidLoad];
categoryArray = [[NSMutableArray alloc] initWithObjects:@"Jack",@"Jone",nil];
}
-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField == myTextField)
{
[numberTextField resignFirstResponder];
if([myTextField.text isEqualToString:@""]){
myTextField.text = [self.categoryArray objectAtIndex:0];
}
NSInteger pickerRow;
for(NSInteger i = 0; i < [self.categoryArray count]; i++){
NSString *string = [self.categoryArray objectAtIndex:i];
if([string isEqualToString:myTextField.text]){
pickerRow = i;
break; //Once we have it break out of the loop
}
}
[picker selectRow:pickerRow inComponent:0 animated:NO];
pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.50];
[UIView setAnimationDelegate:self];
pickrView.frame = CGRectMake(0, 300, pickrView.frame.size.width, pickrView.frame.size.height);
[self.view addSubview:pickrView];
[UIView commitAnimations];
return NO;
}
if([pickrView superview]){
[self animationForPickrDown];
}
return YES;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if([pickrView superview]){
[self animationForPickrDown];
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark
#pragma mark PickrView datasource methods
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.selectedCategory = [NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:row]];
myTextField.text = self.selectedCategory;
[self animationForPickrDown];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
return [self.categoryArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [self.categoryArray objectAtIndex:row];
}
- (void) animationForPickrDown
{
[UIView beginAnimations:nil context:NULL];
pickrView.frame = CGRectMake(0, 300, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView setAnimationDuration:.50];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stopAnimation)];
pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView commitAnimations];
}
- (void) stopAnimation
{
if([pickrView superview]){
[pickrView removeFromSuperview];
}
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
[myTextField release];
[pickrView release];
[selectedCategory release];
}
@end
Thank You
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为其他文本字段设置委托,然后删除条件 if(textField == myTextField)。
并
删除
set delegate for other textfields and then remove condition if(textField == myTextField).
and
remove