Category

Discussion in 'iOS Programming' started by Sergio10, May 11, 2009.

  1. Sergio10 macrumors regular

    Joined:
    Oct 3, 2007
    #1
    Hi,

    I developed category for UIImageView:
    PHP:
    - (NSIntegerval
    {
        return 
    val;
    }
    - (
    voidsetVal: (NSInteger)myValue
    {
        
    val myValue
    }
    And then create 3 images. Set ONLY one setVal with "123". And add to NSArray
    PHP:
    NSMutableArray *myArray = [[NSMutableArray alloc]init];

    UIImageView *view1 =  [[UIImageView allocinitWithFrameCGRectMake(0.00.01010)];
    [
    view1 setVal123];
    [
    myArray addObjectview1];
    UIImageView *view2 =  [[UIImageView allocinitWithFrameCGRectMake(10.00.01010)];
    [
    myArray addObjectview2];
    UIImageView *view3 =  [[UIImageView allocinitWithFrameCGRectMake(20.00.01010)];
    [
    myArray addObjectview3];

    for(
    int a=0a<[myArray count]; a++)
    {
       
    UIImageview *view = [myArray objectAtIndexa];
       
    NSLog(@"Object: %d : %d"a, [view val]);
    }
    So , I get "123" value for all images. WHY? What is wrong???
    Thanks
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    1) Why not just use the pre-defined tag?

    2) How/where have you defined the val variable?
     
  3. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #3
    Here is an interface
    PHP:
    @interface UIImageView (myCat)

    NSInteger val;

    - (
    NSIntegerval;
    - (
    voidsetVal: (NSInteger)some;

    @
    end
     
  4. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    You can't (yet) add instance variables to a class within a category.
     
  5. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #5
  6. Sergio10 thread starter macrumors regular

    Joined:
    Oct 3, 2007
    #6
    And how it works in my code?
    PHP:
    @interface UIImageView (MyCat)
    NSString *title;

    - (
    voidsetTitle: (NSString*) labelText: (NSIntegerfontSize: (UIColor*) textColor;
    - (
    NSString*) title;
    @
    end

    .........

    @
    implementation UIImageView (MyCat)

    - (
    voidsetTitle: (NSString*) labelText: (NSIntegerfontSize: (UIColor*) textColor
    {    
        
    title labelText;
        
    CGRect frame = [self frame];
        
    frame.origin.0;
        
    frame.origin.0;

        
    NSArray *subArray = [self subviews];
        if([
    subArray count] > 0)        // if label allready exist just change text
        
    {
            
    UILabel *label = [subArray lastObject];
            [
    label setFont:[UIFont systemFontOfSizefontSize]];
            
    label.textAlignment UITextAlignmentCenter;
            [
    label setTextlabelText];
            
    label.textColor textColor;
            
    label.backgroundColor = [UIColor clearColor];        
        }
        else    
    // create new label
        
    {
            
    UILabel *label = [[UILabel allocinitWithFrameframe];
            [
    label setFont:[UIFont systemFontOfSizefontSize]];
            
    label.textAlignment UITextAlignmentCenter;
            [
    label setTextlabelText];
            
    label.textColor textColor;
            
    label.backgroundColor = [UIColor clearColor];
            
            [
    self addSubviewlabel];        
        }    
    }

    - (
    NSString*) title
    {
        return 
    title;
    }

     
  7. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #7
    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.
     

Share This Page