Jerry
Jerry

Reputation: 961

UITextField.text keeps returning null

I have create a delegate for this UIViewController, and I am passing in an employee name to set a UITextField. But, no matter what I do, as you can see, it results in (null). However, I have a datelabel that gets populated in a similar way, but it works correctly. I include that for reference. I am missing something simple. But, now I am blind from staring at it...

Any ideas? Thanks.

@interface TimesheetEntryViewController()
@property (weak, nonatomic) IBOutlet UILabel *datelabel;
@property (weak, nonatomic) IBOutlet UITextField *employeeTextField;

@end

@implementation TimesheetEntryViewController
    @synthesize tableView = _tableView;
    @synthesize datelabel = _datelabel;
    @synthesize employeeName = _employeeName;
    @synthesize employeeTextField = _employeeTextField;

-(void)setEmployeeNameString:(NSString *)lemployeeNameString
{
    self.employeeTextField.text = lemployeeNameString;
    NSLog(@"%@:%@", lemployeeNameString, self.employeeTextField.text);
}

Result: 2012-05-02 08:42:50.137 rbTimesheet[7855:11303] Bob Smith:(null)

-(void)changeDateViewController:(ChooseDateViewController *)ntvc didChooseDate:(NSDate     *)lDate
{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    self.datelabel.text = [NSString stringWithFormat:@"%@",
                       [df stringFromDate:lDate]];
    [self dismissModalViewControllerAnimated:YES];
}

Upvotes: 1

Views: 3014

Answers (6)

Prasanna
Prasanna

Reputation: 955

I have mapped the textfield through drag and drop in xcode 5 ,So it is not synthesized but i also tried to resolve this issue by synthesizing it manually but it not worked.

I resolved this issue by assigning empty string in the text field object in viewDidLoad method

_txtDialedNumber.text=@"";

Note:-While using this i am not synthesizing the textfield object and it is working for me in Xcode 5.0(testing in iPhone 5 IOS 6)

Upvotes: 2

Constantine
Constantine

Reputation: 479

So I had the same issue and after googling and coming across multiple posts like this my issue was a fellow developer was doing this(which worked in previous versions of xCode somehow) which I cut and pasted from a similar project

-(void)setTextFieldDelegates{
    self.usernameTextField = [[UITextField alloc] init];
    self.passwordTextField = [[UITextField alloc] init];

    [self.usernameTextField setDelegate: self];
    [self.passwordTextField setDelegate: self];
}

So even though the outlet was connected he was somehow re assigning it and it was working.

Upvotes: 1

Odrakir
Odrakir

Reputation: 4254

Maybe you are calling setEmployeeNameString: before the view is loaded? That should give you null as a result.

Upvotes: 0

Read Q
Read Q

Reputation: 1515

Check if you have subclassed the uitextField by using textField=[[UITextField alloc]init].

If you have done that, try removing it from the code. I was having the same problem and I got it working by removing these lines.

Upvotes: 0

Bill Burgess
Bill Burgess

Reputation: 14154

It looks as if you are overriding the automatic setter/getter for your textfield. The whole point of using @synthesize is to save you some code and hassle from setting them yourself.

My guess is you aren't setting the delegate right. You are saying that the current view controller text field should get the date, then you dismiss the modal view killing the value saved there.

I would change your reference from weak to strong and get rid of your setter. It is being done for you already, no need to clutter things up.

Upvotes: -1

graver
graver

Reputation: 15213

Check if the connection between the text field and the outlet is correct in the Interface Builder.

Upvotes: 0

Related Questions