如何在选择多个文本字段时添加选择器,我已经有一个文本字段正在工作?

发布于 2024-11-30 11:48:38 字数 3514 浏览 2 评论 0原文

我已经在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隱形的亼 2024-12-07 11:48:38

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

删除

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

    return YES;

set delegate for other textfields and then remove condition if(textField == myTextField).

and

remove

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

    return YES;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文