views:

416

answers:

4

I am working on an iPhone app and am getting (null) references to IBOutlet fields in my controller. I have a UIViewController subclass that is set as the File's Owner in my XIB. I have a set of UI elements that are wired into the controller. After loading from NIB and attempting to set properties on those UI elements, I find that they are (null). To clarify, some code:

ExpandSearchPageController.h:

@interface ExpandSearchPageController : UIViewController
{
  IBOutlet UITextView * completeMessageView;
}

-(void)checkTextField;

@property (nonatomic, retain) IBOutlet UITextView * completeMessageView;

ExpandSearchPageController.m:

@implementation ExpandSearchPageController

@synthesize completeMessageView;

-(void)checkTextField
{
  NSLog(@"text field: %@",completeMessageView);
}

ExpandSearchPageController is set as the File's Owner for ExpandSearchPage.xib. ExpandSearchPage.xib's UITextView is wired to the completeMessageView.

When I call

ExpandSearchPageController * searchExpanderPage = [[ExpandSearchPageController alloc] initWithNibName:@"ExpandSearchPage" bundle:[NSBundle mainBundle]];

[searchExpanderPage checkTextField];

the result is

"text field: (null)"

Sorry if this is a total newb question, but I guess I'm still a newb at iPhone programming!

Thanks in advance!

A: 

Make sure the view property of your view controller (ie File's Owner in this case) is wired to the view in your xib. As your textField is almost certainly a subview of that, it's important to get that wired in too (and I don't think the nib will load properly without that being set).

jbrennan
Yes, the view was wired properly.Thanks!
Zach
+3  A: 

I guess asking the question after looking at the problem for over an hour led me to figure it out:

I just changed my code to check the text box AFTER displaying the view... now everything is instantiated.

Imagine that: the UI elements aren't instantiated until you display them!

Zach
A: 

Another potential cause of a null IBOutlet pointer is forgetting to save the xib file in Interface Builder after creating the outlet connection.

smh
A: 

This helped so much. I had been trying to debug a problem for a month, only a couple hours a week, but still wasted a month. It was so frustrating that I did not want to work on the problem. I made my self work on it some more today and gave up. I decide to trying a google this problem, but that was hard as well. Finally I came across this article, and it was my problem. I was trying to set the variables during initialization. This is not a problem for languages like Java which I program in as well, but it is for this *&^% objective C when using NIB or XIB files. So I changed my code to initialize the variables after I added the view as a subview and that worked like a charm. The problem is that is not encapsulated code, you should be able to do that initialization within the initialization process.

ejworks4u