UITextField文本未保存

时间:2014-03-18 08:47:37

标签: ios objective-c uitextfield uitextfielddelegate

我有一些UITextField属性:

 @property (strong, nonatomic) IBOutlet UITextField *phone;

并在.h文件中我已将委托设置为:

<UITextFieldDelegate>

并合成它。

要创建一个文本字段,我将phone发送给一个函数:

  [self createTextFieldWithText:self.phone AndRect:CGRectMake(25, 100, 250, 60) AndText:@"Phone Number"];

即:

-(void)createTextFieldWithText:(UITextField*)text AndRect:(CGRect)rect AndText:(NSString*)inText
{
    text = [[UITextField alloc] initWithFrame:rect];
    text.borderStyle = UITextBorderStyleRoundedRect;
    text.font = [UIFont systemFontOfSize:26];
    text.textColor=[UIColor blackColor];
    text.placeholder = inText;
    text.font=[UIFont fontWithName:@"Apple SD Gothic Neo" size:26];
    text.autocorrectionType = UITextAutocorrectionTypeNo;
    text.keyboardType = UIKeyboardTypeDefault;
    text.returnKeyType = UIReturnKeyDone;
    text.clearButtonMode = UITextFieldViewModeWhileEditing;
    text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [text setDelegate:self];
    [self.view addSubview:text];
}

并有一个委托方法:

-(void)textFieldDidBeginEditing:(UITextField *)mytextField
{
    NSLog(@"did begin");
    mytextField.text=@"";

    //change nack send button color
    [self.phoneB setTitleColor:[UIColor colorWithRed:0/255. green:0/255. blue:0/255. alpha:1] forState:UIControlStateNormal];

}

当我尝试使用该字段时,该字段中的文本未被保存。例如,如果我以后做:

  phone.text  

我内心没有看到任何内容。

1 个答案:

答案 0 :(得分:3)

这是一种比较奇怪的方法调用。 你应该这样做:

self.phone = [self createTextFieldWithRect:CGRectMake(25, 100, 250, 60) AndText:@"Phone Number"];

该方法将启动的文本字段作为输出返回。

-(UITextField *)createTextFieldWithRect:(CGRect)rect AndText:(NSString*)inText
{
    UITextField *text = [[UITextField alloc] initWithFrame:rect];
    text.borderStyle = UITextBorderStyleRoundedRect;
    text.font = [UIFont systemFontOfSize:26];
    text.textColor=[UIColor blackColor];
    text.placeholder = inText;
    text.font=[UIFont fontWithName:@"Apple SD Gothic Neo" size:26];
    text.autocorrectionType = UITextAutocorrectionTypeNo;
    text.keyboardType = UIKeyboardTypeDefault;
    text.returnKeyType = UIReturnKeyDone;
    text.clearButtonMode = UITextFieldViewModeWhileEditing;
    text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [text setDelegate:self];
    [self.view addSubview:text];

    return text;
}

问题是,在当前实现中,您将null对象发送到函数中。 编译器还没有对self.phone对象的引用。 (尚未创建。)