簡體   English   中英

為什么不調用我的UITextField委托方法?

[英]Why is my UITextField delegate method not called?

這是我的問題。 我創建了一個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)。 有人可以幫忙嗎?

- (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;
  }
  1. 您設置了emailTextField的委托,但是在創建UITextField時,設置了textField。 如果將@mailTextField合成為textField,這可能會起作用。

  2. 假設您正在emailTextFiled和textField之間進行@synthesize轉換,或者將emailTextField更改為textField,則在創建對象之前先設置委托。 您需要移動設置委托,直到調用setTextFields之后:

有幾個問題:

您需要在實際創建UITextField之前設置委托。

setTextFields:方法中創建UITextField時,永遠不會將其分配給屬性。 您要做的只是將nil傳遞給您的方法,並且當您為方法中的變量分配值時,您只是在該方法的上下文內分配它。 它不會返回並設置屬性。

可能是InterfaceBuilder構建視圖的方式。

在應用程序啟動時嘗試使用[MyTextFieldDelegate new]。 從NIB喚醒視圖后,您的課程將被注冊並正確綁定到字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM