PDA

View Full Version : Category




Sergio10
May 11, 2009, 07:32 AM
Hi,

I developed category for UIImageView:

- (NSInteger) val
{
return val;
}
- (void) setVal: (NSInteger)myValue
{
val = myValue
}

And then create 3 images. Set ONLY one setVal with "123". And add to NSArray

NSMutableArray *myArray = [[NSMutableArray alloc]init];

UIImageView *view1 = [[UIImageView alloc] initWithFrame: CGRectMake(0.0, 0.0, 10, 10)];
[view1 setVal: 123];
[myArray addObject: view1];
UIImageView *view2 = [[UIImageView alloc] initWithFrame: CGRectMake(10.0, 0.0, 10, 10)];
[myArray addObject: view2];
UIImageView *view3 = [[UIImageView alloc] initWithFrame: CGRectMake(20.0, 0.0, 10, 10)];
[myArray addObject: view3];

for(int a=0; a<[myArray count]; a++)
{
UIImageview *view = [myArray objectAtIndex: a];
NSLog(@"Object: %d : %d", a, [view val]);
}

So , I get "123" value for all images. WHY? What is wrong???
Thanks



robbieduncan
May 11, 2009, 07:34 AM
1) Why not just use the pre-defined tag (http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instp/UIView/tag)?

2) How/where have you defined the val variable?

Sergio10
May 11, 2009, 07:42 AM
Here is an interface

@interface UIImageView (myCat)

NSInteger val;

- (NSInteger) val;
- (void) setVal: (NSInteger)some;

@end

eddietr
May 11, 2009, 09:09 AM
You can't (yet) add instance variables to a class within a category.

jnic
May 11, 2009, 09:28 AM
Specifically:

http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCategories.html

Note that a category can’t declare additional instance variables for the class; it includes only methods.

As Robbie says, why not just use .tag?

Sergio10
May 12, 2009, 04:35 PM
Note that a category can’t declare additional instance variables for the class; it includes only methods.


And how it works in my code?

@interface UIImageView (MyCat)
NSString *title;

- (void) setTitle: (NSString*) labelText: (NSInteger) fontSize: (UIColor*) textColor;
- (NSString*) title;
@end

.........

@implementation UIImageView (MyCat)

- (void) setTitle: (NSString*) labelText: (NSInteger) fontSize: (UIColor*) textColor
{
title = labelText;
CGRect frame = [self frame];
frame.origin.x = 0;
frame.origin.y = 0;

NSArray *subArray = [self subviews];
if([subArray count] > 0) // if label allready exist just change text
{
UILabel *label = [subArray lastObject];
[label setFont:[UIFont systemFontOfSize: fontSize]];
label.textAlignment = UITextAlignmentCenter;
[label setText: labelText];
label.textColor = textColor;
label.backgroundColor = [UIColor clearColor];
}
else // create new label
{
UILabel *label = [[UILabel alloc] initWithFrame: frame];
[label setFont:[UIFont systemFontOfSize: fontSize]];
label.textAlignment = UITextAlignmentCenter;
[label setText: labelText];
label.textColor = textColor;
label.backgroundColor = [UIColor clearColor];

[self addSubview: label];
}
}

- (NSString*) title
{
return title;
}

eddietr
May 12, 2009, 05:16 PM
And how it works in my code?

If that is working for you, then I think that's a bug in the compiler. If the implementation of UIImageView changes in the future, your app should crash, or at least behave very strangely.