如何重用UILabel? (或任何物体)
这:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不能在同一块级作用域中使用相同的变量名称。因此,在第一个示例中,您不能拥有具有相同名称的变量定义,您必须以不同的方式命名它们。
在您的循环示例中,您处于一个新范围内,每次迭代后都会重新初始化该范围。
您的第三个示例是正确的,因为它仅定义了该变量一次。之后您可以重用此变量来分配新对象。第三个不太优雅,因为您的变量名称不能很好地描述每种情况的用途。
对于“Sign = nil”的情况,这实际上使后面的行变得无用,因为在 Objective-C 中,发送到 nil 对象的消息被忽略。
我建议定义一个方法,您可以调用该方法来创建看起来相同的图像。像这样的东西:
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.
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:
你的 for 循环完全没问题。 myLabel 的范围仅限于 for 循环的一次运行。因此,每次运行时都会创建一个新变量来保存对 UILabel 的引用。
您发布的第二个代码有泄漏。
这将释放地址为 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.
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.