PDA

View Full Version : Custom Class not Linking to other Objects




Jman012
Oct 5, 2010, 01:05 AM
I have so far a simple application where when a TableViewCell is pressed, a custom class will be out on the screen. The class, Element, is derived from UIView, and draws two ImageViews and a Label programmatically, and it can be dragged around. These are the basic and important parts of the custom class:

// Element.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@class Element;

@protocol ElementDelegate <NSObject>
@required
- (void)elementWasTouchedWithName:(NSString *)name andID:(int)ID;
@end

@interface Element : UIView {

IBOutlet UIImageView *topPlacementCombo;
IBOutlet UIImageView *middlePlacementCombo;
IBOutlet UIImageView *bottomPlacementCombo;

IBOutlet UILabel *one;
IBOutlet UILabel *two;
IBOutlet UILabel *three;
}

@property (nonatomic, retain) IBOutlet UIImageView *topPlacementCombo;
@property (nonatomic, retain) IBOutlet UIImageView *middlePlacementCombo;
@property (nonatomic, retain) IBOutlet UIImageView *bottomPlacementCombo;

@property (nonatomic, retain) IBOutlet UILabel *one;
@property (nonatomic, retain) IBOutlet UILabel *two;
@property (nonatomic, retain) IBOutlet UILabel *three;



- (void)setDelegate:(id <ElementDelegate>)dlg;
- (void)loadVisualViews;

@end



//Element.m
#import "Element.h"

@implementation Element

@synthesize topPlacementCombo, middlePlacementCombo, bottomPlacementCombo;
@synthesize one, two, three;

- (void)setDelegate:(id <ElementDelegate>)dlg {
delegate = dlg;
}

- (id)init {
if((self = [super init])){
self.userInteractionEnabled = TRUE;
[self setBackgroundColor:[UIColor clearColor]];

}
return self;
}

- (void)loadVisualViews {
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
[backgroundImage setImage:[UIImage imageNamed:@"ElementBackground2.png"]];
[backgroundImage setOpaque:TRUE];
[self addSubview:backgroundImage];
[backgroundImage release];

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 38, 64, 21)];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setAdjustsFontSizeToFitWidth:TRUE];
[label setTextAlignment:UITextAlignmentCenter];
[label setText:elementName];
[self addSubview:label];
[label release];

UIImageView *mainImage = [[UIImageView alloc] initWithFrame:CGRectMake(16, 5, 32, 32)];
[mainImage setImage:[UIImage imageNamed:@"SampleElementIcon2.png"]];
[mainImage setOpaque:TRUE];
[self addSubview:mainImage];
[mainImage release];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint pt = [[touches anyObject] locationInView:self];
startPosition = pt;
[[self superview] bringSubviewToFront:self];
[delegate elementWasTouchedWithName:elementName andID:elementID];

one.text = @"Blah";
NSLog(@"Text: %@", one.text);

[topPlacementCombo setHighlighted:TRUE];
NSLog(@"%@", topPlacementCombo.highlighted);
}

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

@end



And how I allocate/initialize it:

Element *element = [[Element alloc] init];
element.frame = CGRectMake(32, 32, 64, 64);
element.elementName = elementName;
[element loadVisualViews];
[self.boardView addSubview:element];
[element release];


And I provided pictures for how I linked it, but I basically used an object, and gave it the class Element.

But the big problem is is that the links do not work. The couple NSLogs in touchesBegan only spit out (null). Have I set up my Element class wrong? Did I link it wrong? Or is it just Xcode4?
Much help would be greatly appreciated, and I hope I haven't wasted your time.

Also if it's worth anything, here's my parent tree:

Window
ViewController View
UIView
3 UILabels | UIView
3 UIImageViews, UIButton

The '|' representing a branch in the tree...

Anyways didn't know if it would make a difference.



ianray
Oct 5, 2010, 02:27 PM
The couple NSLogs in touchesBegan only spit out (null).

This happens because the objects are null -- they have not been initialized anywhere.

The use of IBOutlet tags in your .h file suggests that you were expecting InterfaceBuilder to create the objects for you -- if so, then a NIB file must be loaded :)

The screenshots are XCode4 and anecdotal evidence from Twitter suggests that XCode4 is not ready for production use, so it is definitely worth switching back to XCode3 (even though that is not the root cause of your problem).

Jman012
Oct 5, 2010, 05:25 PM
Xcode4 works fine and smooth aside from a few small bugs and the Xcode team has synced IB and Xcode4 beautifully.

But doesn't initialize the UI objects for you? I remember dealing with simply linking to an object in IB and be able to get/set any variables right off the bat. I'm not saying that you're wrong, but it seems far fetched, I'll give it a try. Thanks for posting.

ianray
Oct 6, 2010, 02:58 PM
The code you provided simply instantiates Element without loading a NIB file.

All I am saying is that if you are expecting variables tagged with 'IBOutlet' to be initialized for you, then you need to load a NIB file. Sorry for not being clearer before :)

Jman012
Oct 6, 2010, 05:56 PM
Ah, that makes much more sense, thank you so much. Is there a possibility of you providing some example for doing that? If not I know where t get more help.

ianray
Oct 7, 2010, 02:11 PM
It's unclear to me how you expect the NIB objects to co-exist with backgroundImage, label, and mainImage -- and indeed how loadVisualViews co-exists with the NIB; but, initWithNibName is the UIViewController method that I was referring to.