如何在选择多个文本字段时添加选择器,我已经使用了一个TextField吗?

时间:2011-08-18 11:50:51

标签: iphone sdk uitextfield uipickerview

我已经在我的Nib文件上连接了三个TextField插座

IBOutlet UITextField *myTextField;
IBOutlet UITextField *myTextFieldd;
IBOutlet UITextField *myTextFields;

现在,当我选择myTextFieldd和myTextFields时,我正在尝试添加一个Picker的选择器,请注意myTextField工作得很好。我也试图使用相同的选择器来弹出三个TextFields。

代码:
     #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

谢谢

1 个答案:

答案 0 :(得分:0)

为其他文本字段设置委托,然后删除条件if(textField == myTextField)。

删除

if([pickrView superview]){
    [self animationForPickrDown];
}

    return YES;