Question

I have some UITextField property :

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

and in the .h file i have set the delegate with :

<UITextFieldDelegate>

and synthesise it.

Than to create a text field i send that phone to a function :

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

that is:

-(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];
}

and have a delegate method:

-(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];

}

what happens is that the text in that field is not being saved after i am trying to use it. for example if i later do :

  phone.text  

I see nothing inside.

Was it helpful?

Solution

It is rather strange method call. You should do this instead:

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

And the method returns the initiated text field as output.

-(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;
}

The thing is, in the current implementation, you are sending the null object into the function. The compiler would not have the reference to the self.phone object yet. (It has not been created.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top