简体   繁体   中英

UITextField outlets null at runtime

I'm having a strange issue in my storyboard-based XCode project where UITextFields outlets in my view are null at runtime (ie the value of their pointer in the Locals window when debugging shows up as "0x00000000"). Delegate methods on the UIViewController are called fine and the 'sender' argument is a valid object, but the property of the same field in "self" is a null pointer. The following is an example of the delegate code where I've broken into in the debugger.

(BOOL) textFieldShouldReturn:(UITextField *) sender {
    // sender is '0x7812...', but self.txtUserName is '0x0000...'
    if(sender == self.txtUserName)
    ....

I was beginning to suspect I must be doing something wrong but I started up another storyboard-based project, added a textfield, hooked it all up... and it worked. Went back to my project, added a textfield, hooked it all up... still '0x00000000'.

Any ideas as to what might cause this sort of thing to happen?

Edit: It appears this happens to any new view object I add; I just tried adding a second UIButton and it has the same problem; the first button I added is just fine. So at some point things have started breaking when adding/linking view objects.

2nd Edit: I just wound up starting a new project and adding the files to it. So far so good.

This might be a very late answer... But the same thing was happening to me for whatever reason... (I am using xcode 5). After playing around a little, I noticed that the debugger where it would say _UITextFielVariable 0x00000000 was always the first UITextField variable in the header file.

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *vehicleType;
@property (strong, nonatomic) IBOutlet UITextField *trackType;
@property (strong, nonatomic) IBOutlet UITextField *massBase;

Here "vehicleType" would always have that issue, however, all I did was add a dummy variable that wasn't linked to anything (like label, text, button, etc.) like this

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *extra;
@property (strong, nonatomic) IBOutlet UITextField *vehicleType;
@property (strong, nonatomic) IBOutlet UITextField *trackType;
@property (strong, nonatomic) IBOutlet UITextField *massBase;

and running the code worked... I really don't know why, if anyone could let me know that would be great.

I am also answering this in case anyone else is looking for this answer and falls on this tread :)

I got this issue when I was pushing a view controller with a storyboard-id after some redesign..

I did a simple screen, assigned a storyboard-id, wrote some code etc..

Then I reworked the screen, rewrote the code, initially using segues but eventually pushing the vc programmatically with the storyboard-id. as the storyboard id corresponded to the refactored class, but some other screen, the correct code was called but none of the outlets were initialised

So if you're loading the screen with a storyboard-id double-check that value corresponds to the screen you expect..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM