将UITextField委托设置为所有UITextFields并为少数文本字段设置视图动画

时间:2016-03-10 11:26:46

标签: ios objective-c cocoa-touch uitextfield

我有13个文本字段。我想只为4个文本字段设置视图动画,这些字段将显示在底部

这是我的代码:

if (textField.editing != ((_GET_companyname)||(_GET_firstname)||(_GET_middlename)||(_GET_lastname)||(_GET_companyurl)||(_GET_streetaddress)||(_GET_postbox)||(_GET_code)||(_GET_phonenum)))
{
    NSLog(@"Entered in condition");
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        //Keyboard becomes visible
        [UIView beginAnimations:nil context:NULL];
        // [UIView setAnimationDuration:0.25];
        self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
        [UIView commitAnimations];
    }
}
else
{
    NSLog(@"Entered else Part");
}

我需要将以下内容设置为所有14个文本域

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}

2 个答案:

答案 0 :(得分:1)

根据你的意见" _GET_companyname"是UITextField的IBOutlet。

然后你应该写下面这样的东西

if ((textField != _GET_companyname)&&(textField != _GET_firstname)) // etc
{
    NSLog(@"Entered in condition");
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        //Keyboard becomes visible
        [UIView beginAnimations:nil context:NULL];
        // [UIView setAnimationDuration:0.25];
        self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
        [UIView commitAnimations];
    }
}
else
{
    NSLog(@"Entered else Part");
}

编辑: - 而不是检查(!=)。你应该检查(==)几个UITextFields。那会更好。

答案 1 :(得分:0)

您可以使用标签检查这些内容 - (void)viewDidLoad {     [super viewDidLoad];

UITextField *textFiledName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledName.tag = 0;
textFiledName.delegate = self;
[self.view addSubview:textFiledName];

UITextField *textFiledLastName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledLastName.tag = 1;
textFiledLastName.delegate = self;
[self.view addSubview:textFiledLastName];

}

 - (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.tag == 0) {
  //No Animation
}else  if (textField.tag == 1){
   //Add Animation
}
return YES;

}