【问题标题】:Why is my UITextField delegate method not called?为什么我的 UITextField 委托方法没有被调用?
【发布时间】:2014-01-30 18:41:42
【问题描述】:

这是我的问题。我创建了一个 UITextField。

.h 文件:

@property (strong, nonatomic) UITextField *emailTextField;

.m 文件:

- (void)viewDidLoad
{
  [super viewDidLoad];
  self.emailTextField.delegate=self;
  [self setTextFields:120 :@"  email" :self.emailTextField];
}

-(void)setTextFields:(float)ycoord :(NSString *)text :(UITextField *)textField
{
  CGRect frame = CGRectMake(60, ycoord, 180, 30);
  textField = [[UITextField alloc]initWithFrame:frame];
  textField.backgroundColor = [UIColor whiteColor];
  textField.placeholder = text;
  textField.font = [UIFont fontWithName:@"Baskerville" size:15];
  textField.allowsEditingTextAttributes=YES;
  textField.autocorrectionType = UITextAutocorrectionTypeNo;

  CALayer *lay = [textField layer];
  [lay setCornerRadius:5.0f];
  [self.view addSubview:textField];
}

目的是保存用户在 TextField 中输入的文本。我尝试了委托方法

-(void)textFieldDidEndEditing:(UITextField *)textField

但是没有调用该方法(我放了一个 NSLog 来检查)。有人可以帮忙吗?

【问题讨论】:

    标签: ios objective-c text delegates uitextfield


    【解决方案1】:
    - (void)viewDidLoad
     {
      [super viewDidLoad];
    
      self.emailTextField= [self setTextFields:120 :@"  email"];
      self.emailTextField.delegate=self;
    
      self.passwordTextField= [self setTextFields:200 :@"  password"];
      self.passwordTextField.delegate=self;
    
      self.nameTextField= [self setTextFields:250 :@"  name"];
      self.nameTextField.delegate=self;
     }
    
    -(UITextField *)setTextFields:(float)ycoord :(NSString *)text{
    
      CGRect frame = CGRectMake(60, ycoord, 180, 30);
    
      UITextField* textField = [[UITextField alloc]initWithFrame:frame];
      textField.backgroundColor = [UIColor whiteColor];
      textField.placeholder = text;
      textField.font = [UIFont fontWithName:@"Baskerville" size:15];
      textField.allowsEditingTextAttributes=YES;
      textField.autocorrectionType = UITextAutocorrectionTypeNo;
    
      CALayer *lay = [textField layer];
      [lay setCornerRadius:5.0f];
      [self.view addSubview:textField];
      return textField;
      }
    

    【讨论】:

    • 我还有其他文本字段(self.passwordTextField 和 self.nameTextField)。这就是我使用函数的原因。所以我不能把你的台词放到我的代码中。
    【解决方案2】:
    1. 您为 emailTextField 设置了委托,但在创建 UITextField 时设置了 textField。如果你 @synthesize emailTextField to textField 这可能会起作用。

    2. 假设您在 emailTextFiled 和 textField 之间进行 @synthesize 或将 emailTextField 更改为仅 textField,您在创建对象之前设置委托。您需要在调用 setTextFields 之前设置委托:

    【讨论】:

      【解决方案3】:

      有几个问题:

      您在实际创建 UITextField 之前设置委托。

      当您在 setTextFields: 方法中创建 UITextField 时,您永远不会将其分配给您的属性。您所做的只是将nil 传递给您的方法,当您为方法中的变量分配值时,您只是在该方法的上下文中分配它。它不会返回并设置属性。

      【讨论】:

        【解决方案4】:

        可能是 InterfaceBuilder 构建视图的方式。

        在应用程序启动时尝试 [MyTextFieldDelegate new]。 当您的视图从 NIB 唤醒时,您的类将被注册并正确绑定到字段。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-26
          • 1970-01-01
          • 2011-04-13
          • 1970-01-01
          相关资源
          最近更新 更多