How do I reuse UILabel? (Or any object)

It:

    UILable *myLabel = [[UILabel alloc] init];
    UILable *myLabel = [[UILabel alloc] init];

      

gives me an override error.

But this:

    for(i=0;i<5;i++)
    { 
       UILable *myLabel = [[UILabel alloc] init];
       // some label code here
       [self.view addSubview:myLabel];
       [myLabel release];
    }

      

not. So the second is false? Should I define it earlier and just reuse it?

Is it correct:

 UIIMageView *Sign;
//Some Sign Stuff
     Sign = [[UIImageView alloc]init];
        Sign.image = [UIImage imageNamed:@"Minus.png"]; 
        frame = CGRectMake(160 ,80, 64, 64);
        Sign.frame = frame;
        [scrollView addSubview:Sign];
        Sign = nil;
        [Sign release];
//Some other Sign stuff
     Sign = [[UIImageView alloc]init];
        Sign.image = [UIImage imageNamed:@"Plus.png"];  
        frame = CGRectMake(200 ,80, 64, 64);
        Sign.frame = frame;
        [scrollView addSubview:Sign];
        Sign = nil;
        [Sign release];

      

it is right? It doesn't work without Sign = nil. So it seems a little shaky.

+2


source to share


2 answers


You cannot have the same variable names used in the same block level scope. Therefore, in the first example, you cannot have a variable definition with the same name, you must call them differently.

- (void) method {
   UIImageView* image1;

   // here we define a new block scope. This can be a block of any kind (while, for, if)
   {
      // All reference in this block to this variable will see this definition.
      UIImageView* image1;

      // Using image1 here
   }

   // Here we see again the image1 defined at the beginning of the method.
}

      

In your example loop, you are in a new scope that it reinitializes after each iteration.

Your third example is correct as it only defines the variable once. After that, you reuse this variable to assign a new object. The third one is less graceful, since your variable name doesn't describe well for every case what their purpose is.



For your case, "Sign = nil" this actually makes the line that follows useless, since in Objective-C the message sent to the nil object is ignored.

I would suggest defining a method that you can call to create your images that look the same. Something like:

- (void) createImage:(NSString*) image frame:(CGRect) frame {
  UIImageView *Sign;
  Sign = [[UIImageView alloc]init];
  Sign.image = [UIImage imageNamed:image]; 
  Sign.frame = frame;
  [self.scrollView addSubview:Sign];
  [Sign release];
}

      

+1


source


Your for-loop works great. The scope of myLabel is limited to one run for your loop. So each one starts a new variable to store the reference to your UILabel.

The second code you posted has leaks.

Sign = nil
[Sign release]

      



This will free the object at nil, not the object you are creating. I don't see what else is wrong with your code, but your fix certainly does not address the root cause. Perhaps this will help post what error / warning you get when removing Sign = nil.

Also note that capitalizing variable names is not a good naming convention because usually class names start with one.

0


source







All Articles