如何重用UILabel? (或任何物体)

发布于 2024-08-06 16:59:35 字数 1061 浏览 3 评论 0原文

这:

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

给我一个重新定义错误。

但这:

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

没有。那么第二个是假的吗?我应该先定义它然后重用它吗?

是这样吗:

 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];

这样正确吗?如果没有 Sign = nil,这将不起作用。所以看起来也有点摇摇欲坠。

This:

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

gives me a redefinition error.

But this:

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

doesn't. So is the second one false? Should I define it before and just reuse it?

Is that right:

 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];

is that correct? That doesnt work without the Sign = nil. So it seems a little wobbly too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2024-08-13 16:59:35

不能在同一块级作用域中使用相同的变量名称。因此,在第一个示例中,您不能拥有具有相同名称的变量定义,您必须以不同的方式命名它们。

- (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.
}

在您的循环示例中,您处于一个新范围内,每次迭代后都会重新初始化该范围。

您的第三个示例是正确的,因为它仅定义了该变量一次。之后您可以重用此变量来分配新对象。第三个不太优雅,因为您的变量名称不能很好地描述每种情况的用途。

对于“Sign = nil”的情况,这实际上使后面的行变得无用,因为在 Objective-C 中,发送到 nil 对象的消息被忽略。

我建议定义一个方法,您可以调用该方法来创建看起来相同的图像。像这样的东西:

- (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];
}

You cannot have identical variable names used in the same block level scope. So in your first example you cannot have a variable definition with the same name, you have to name 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 loop example you are in a new scope that it's reinitialize after each iteration.

Your third example is correct in that it define the variable only one time. You reuse this variable after that to assign a new object. The third one is less elegant in that your variable name does not describe well for each case what are their purpose.

For your case of 'Sign = nil' this effectively make the line that follows useless since in Objective-C a message sent to a nil object is ignored.

I would suggest to define 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];
}
梦毁影碎の 2024-08-13 16:59:35

你的 for 循环完全没问题。 myLabel 的范围仅限于 for 循环的一次运行。因此,每次运行时都会创建一个新变量来保存对 UILabel 的引用。

您发布的第二个代码有泄漏。

Sign = nil
[Sign release]

这将释放地址为 nil 的对象,而不是您创建的对象。我看不出您的代码还有什么问题,但您的修复绝对不能解决根本原因。也许它将有助于发布删除 Sign = nil 时收到的错误/警告。

另请注意,以大写字母开头的变量名并不是一个好的命名约定,因为通常类名以 1 开头。

Your for-loop is perfectly fine. The scope of myLabel is limited to one run of your for-loop. So each run a new variable to hold the reference to your UILabel is created.

The second code you posted has leaks.

Sign = nil
[Sign release]

This will release the object at address nil and not the object you created. I can't see what else is wrong with your code, but your fix is definitely not fixing the root cause. Maybe it will help to post what error/warning you get when removing Sign = nil.

Also note that starting your variable names with a capital letter is not a good naming convention, because usually class names start with one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文