0

OK I'm pulling my hair on on this, it makes no sense so it must either be a bug, or some extremely stupid mistake I'm making.

Here's the situation: I have a UIViewController with a xib file. It has a UIView hooked up to the view property, a UIImageView hooked up to a property called imageView, and a UISegmentedControl hooked up to segmentControl.

Yet in my code, I'm unable to assign an image to the image view. When I NSLog the object, I'm getting the address of 0x0, so it's not associating at all. I have the bindings set up in Interface builder, I've restarted XCode and clean and built the project. Nothing is working and I've wasted a stupid amount of time trying to figure this out at this point. I've also NSLoged the segmentControl object and am getting 0x0 as well. So nothing from the XIB is hooking up with the code, but the XIB itself is loading because the objects are displayed on the screen.

Does anyone have any ideas offhand of what I could be missing? This is such a simple thing, it's incredibly frustrating that it's refusing to work all of a sudden.


Here's the code below, in this case I'm accessing the objects in the setter for projectId, but I also tried accessing the imageView from another class and it didn't work either (and yes I had a @property declaration and synthesize for imageView when I tried).

DiagramViewController.h:

#import <UIKit/UIKit.h>


@interface DiagramViewController : UIViewController 
{
    NSUInteger projectId;
    IBOutlet UIImageView *imageView;
    IBOutlet UISegmentedControl *segmentControl;
}

@property NSUInteger projectId;

@end

DiagramViewController.m:

#import "DiagramViewController.h"

@implementation DiagramViewController
@synthesize projectId;

-(void)setProjectId:(NSUInteger)projId
{
    NSLog(@"imageView: %@", imageView);
    NSLog(@"segmentControl: %@", segmentControl);

    projectId = projId;

    NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%i_schem", projectId] ofType:@"png" inDirectory:@"project-details/images/diagrams"];
    NSData *imageData = [NSData dataWithContentsOfFile:filePath];

    imageView.image = [UIImage imageWithData:imageData];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
}

@end

And here's an image of the Interface Builder hookups: http://i52.tinypic.com/2eakprl.png

2
  • Post some code from your controller, where these things are nil, and where you're setting them up. My first inkling is that you're using getter properties inside -loadView, but I'd like to see some code so we can properly answer this. Commented Nov 10, 2010 at 22:19
  • Updated the project with the code and IB screenshot, this makes 0 sense, it's the simplest view controller I've ever put together and just refuses to work. Commented Nov 10, 2010 at 22:45

2 Answers 2

3

You need to make sure you're doing this in the -viewDidLoad method, otherwise the nib file hasn't actually unarchived the objects and thus they will be set to nil. In particular, you can't access items from the nib within an -init method.

Without seeing your code however, it's hard to say if this is the case for you.

EDIT: Ok, thanks for posting code. When does -setProjectId: get invoked? Are you sure the nib has finished loading at this point? You have no viewDidLoad method in your view controller so it appears you never check for this at any time.

See: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

viewDidLoad

Called after the controller’s view is loaded into memory.

  • (void)viewDidLoad

Discussion

This method is called after the view controller has loaded its associated views into memory. This method is called regardless of whether the views were stored in a nib file or created programmatically in the loadView method. This method is most commonly used to perform additional initialization steps on views that are loaded from nib files.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Moved the logic into the viewWillAppear: method and it's working now. I usually construct my view controllers in the viewDidLoad or viewWillAppear: methods, not sure why I was doing it differently this time. I knew it had to be some stupid mistake I was making. Thanks again!
0

Sounds like something went wrong when you wired up the IBOutlets.

In IB right click on the UISegmentedControl and post a screenshot so that we can see what you have there. If you see anything 'yellow' then you may have found your problem.

4 Comments

Afraid it's not that simple. I've checked everything obvious. There is no reason that I can see that this isn't working. It's really pissing me off.
See the update to my post. I've included code and a screenshot from IB
It's highly unlikely that the objects aren't being loaded. It's far more likely that they aren't loaded at the time they're being accessed. When working with nibs you need to make sure you implement the methods the provide feedback about the state of the nib contents (i.e. -viewDidLoad in UIViewController).
Declare properties and synthesize the segmentedControl and imageView, maybe that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.