从视图中删除 UILabel 时出现问题

发布于 2024-11-13 15:36:32 字数 1707 浏览 1 评论 0原文

我认为有一个 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 技术交流群。

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

发布评论

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

评论(2

记忆之渊 2024-11-20 15:36:32

您列出的所有方法都有效。所以我担心您没有在正确的位置搜索错误。

确保:

  1. 您获得的 label 实例是好的实例,
  2. 只有一个 label 实例(您可以删除不小心插入的重复项...)

一个非常调试此类问题的有效方法是(我每天都会这样做很多次...):

  1. 在要删除标签的位置放置一个断点
  2. 当断点命中时,在中输入该命令gdb 控制台:

    <前><代码>> po [[自我视图]递归描述]

    <块引用>

    recursiveDescription 将显示 [self view] 子视图树,其中包含地址和许多有用的信息,例如坐标...隐藏...这将使您清楚地了解该视图中发生的情况。

  3. 转到下一步,在调用 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 :

  1. the label instance you are getting is the good one
  2. there is only one label 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...) :

  1. put a breakpoint where you are removing the label
  2. when breakpoint hit, enter that command in gdb console :

    > po [[self view] recursiveDescription]
    

    recursiveDescription will display a tree of [self view] subviews, with adresses and many useful informations like coordinates... hidden... That will allow you to clearly understand what is going on in that view.

  3. 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 :)

狼性发作 2024-11-20 15:36:32

所有代码都在一个方法中吗?如果是..您正在创建新的 startLabelstartLabel2 并尝试在代码的 else 部分中删除它们,但这些不是您添加为子视图的标签。将标签作为类的成员,并且仅在 init/viewDidLoad 方法中分配它们一次。

is all that code in a single method? If yes.. you're creating new startLabel and startLabel2 and try to remove them in the else 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 the init/viewDidLoad method.

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