【问题标题】:Set UITextField delegate to all UITextFields and set view animation for few textfields将 UITextField 委托设置为所有 UITextField 并为少数文本字段设置视图动画
【发布时间】:2016-06-25 05:13:52
【问题描述】:

我有 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;
}

【问题讨论】:

  • 什么是_GET_companyname 和其他?
  • 那么问题出在哪里?
  • 我有 13 个文本字段我想将文本视图设置为全部并将 Uiview 动画设置为 4 个文本字段@Andy
  • GET 等是视图控制器@Kiran 中的文本字段
  • 那你为什么要检查(textField.editing)?相反,您应该只检查(文本字段)

标签: ios objective-c cocoa-touch uitextfield


【解决方案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。这样会更好。

【讨论】:

  • 即使我执行要在 else 部分执行的操作,也始终进入 if 块
  • 您必须根据需要更新此代码,我还对其进行了编辑并在其间添加了 (&&) 条件
【解决方案2】:

您可以使用标签检查这些 - (void)viewDidLoad { [超级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;

}

【讨论】:

  • 终于成功了 if (textField.tag > 8) { [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 提交动画]; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多