从视图中删除 UILabel 时出现问题
我认为有一个 UILabel 。如果正在调用某个函数,我想删除 UILabel。我已经尝试了几种方法,但它不起作用!
尝试过的代码:
[[self.view viewWithTag:1] setHidden:YES];
或者
label.hidden=YES;
或者
[label removeFromSuperview];
我已经尝试了所有这些代码,但没有成功。有人可以让我知道我在这里缺少什么吗?谢谢。
我的代码的其他部分:
UILabel *startLabel=[[[UILabel alloc] initWithFrame:CGRectMake(55, 45, 300, 30)] autorelease];
UILabel *startLabel2=[[[UILabel alloc] initWithFrame:CGRectMake(40, 65, 300, 30)] autorelease];
if ((internetActive==NO)||(hostActive==NO)) {
status=1;
//add the label
startLabel.text=@"Cannot connect to server.";
startLabel.backgroundColor=[UIColor clearColor];
startLabel.textColor=[UIColor whiteColor];
startLabel.font=[UIFont systemFontOfSize:18];
startLabel.tag=1;
[self.view addSubview:startLabel];
startLabel2.text=@"Please check your connection.";
startLabel2.backgroundColor=[UIColor clearColor];
startLabel2.textColor=[UIColor whiteColor];
startLabel2.font=[UIFont systemFontOfSize:18];
startLabel2.tag=2;
[self.view addSubview:startLabel2];
startLabel.hidden=NO;
startLabel2.hidden=NO;
}
else if ((internetActive==YES) &&(hostActive==YES))
{
if(status==1)
{
//[startLabel removeFromSuperview];
//[startLabel2 removeFromSuperview];
//[[self.view viewWithTag:1] setHidden:YES];
//[[self.view viewWithTag:2] setHidden:YES];
NSLog(@"INSIDE!");
startLabel.hidden=YES;
startLabel2.hidden=YES;
[startLabel removeFromSuperview];
[startLabel2 removeFromSuperview];
}
I have a UILabel in my view. I want to remove the UILabel if a certain function is being called. I have tried a couple of ways, but its not working!
Codes tried:
[[self.view viewWithTag:1] setHidden:YES];
or
label.hidden=YES;
or
[label removeFromSuperview];
I have tried all of these codes, but to no avail. Cany anyone kindly let me know what I am missing here ? Thanks.
Other part of my code:
UILabel *startLabel=[[[UILabel alloc] initWithFrame:CGRectMake(55, 45, 300, 30)] autorelease];
UILabel *startLabel2=[[[UILabel alloc] initWithFrame:CGRectMake(40, 65, 300, 30)] autorelease];
if ((internetActive==NO)||(hostActive==NO)) {
status=1;
//add the label
startLabel.text=@"Cannot connect to server.";
startLabel.backgroundColor=[UIColor clearColor];
startLabel.textColor=[UIColor whiteColor];
startLabel.font=[UIFont systemFontOfSize:18];
startLabel.tag=1;
[self.view addSubview:startLabel];
startLabel2.text=@"Please check your connection.";
startLabel2.backgroundColor=[UIColor clearColor];
startLabel2.textColor=[UIColor whiteColor];
startLabel2.font=[UIFont systemFontOfSize:18];
startLabel2.tag=2;
[self.view addSubview:startLabel2];
startLabel.hidden=NO;
startLabel2.hidden=NO;
}
else if ((internetActive==YES) &&(hostActive==YES))
{
if(status==1)
{
//[startLabel removeFromSuperview];
//[startLabel2 removeFromSuperview];
//[[self.view viewWithTag:1] setHidden:YES];
//[[self.view viewWithTag:2] setHidden:YES];
NSLog(@"INSIDE!");
startLabel.hidden=YES;
startLabel2.hidden=YES;
[startLabel removeFromSuperview];
[startLabel2 removeFromSuperview];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您列出的所有方法都有效。所以我担心您没有在正确的位置搜索错误。
确保:
label
实例是好的实例,label
实例(您可以删除不小心插入的重复项...)一个非常调试此类问题的有效方法是(我每天都会这样做很多次...):
当断点命中时,在中输入该命令
gdb
控制台:<前><代码>> po [[自我视图]递归描述]
<块引用>
recursiveDescription
将显示[self view]
子视图树,其中包含地址和许多有用的信息,例如坐标...隐藏...这将使您清楚地了解该视图中发生的情况。转到下一步,在调用
removeFromSuperview
后,再次执行该命令,看看发生了什么变化……或者没有发生变化。仔细检查对象地址。学习一般的 gdb 和调试,你就再也不用问这类问题了:)
All the methods you listed works. So I'm afraid that you are not searching your bug at the right place.
Make sure that :
label
instance you are getting is the good onelabel
instance (you could just removing a duplicate that you accidentally inserted...)One very efficient way to debug these kind of issues is (I do this many times a day...) :
when breakpoint hit, enter that command in
gdb
console :go to next step, after you
removeFromSuperview
call, play again the command, and look what changed... or not. Check objects adresses carefully.Learn gdb and debugging in general, and you will never have to ask these kind of questions again :)
所有代码都在一个方法中吗?如果是..您正在创建新的
startLabel
和startLabel2
并尝试在代码的else
部分中删除它们,但这些不是您添加为子视图的标签。将标签作为类的成员,并且仅在init/viewDidLoad
方法中分配它们一次。is all that code in a single method? If yes.. you're creating new
startLabel
andstartLabel2
and try to remove them in theelse
part of the code, but those are not the labels that you added as subviews. Make the labels as member of the class, and only alloc them once in theinit/viewDidLoad
method.