在 Init 上隐藏 UILabel

发布于 2024-12-14 13:40:32 字数 432 浏览 1 评论 0原文

我对这个基本代码有疑问:

-(id)init{
self = [super init];
if(self){
    self.mensaje = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [self.mensaje setText:@"He vuelto"];

    [self.view addSubview:self.mensaje];
    [self.mensaje setHidden:YES];
}
return self;
}

除了 [self.mensaje setHidden:YES]; 之外,所有代码都工作正常。标签始终在开始时显示。

我希望能帮助我,这是基本的,但必要的!

祝你好运!

I'm having a problem with this basic code:

-(id)init{
self = [super init];
if(self){
    self.mensaje = [[UILabel alloc]initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [self.mensaje setText:@"He vuelto"];

    [self.view addSubview:self.mensaje];
    [self.mensaje setHidden:YES];
}
return self;
}

All the code works fine, except [self.mensaje setHidden:YES];. The Label is always shown at start.

I hope could help me, this is basic, but necessary!!

Good luck!

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

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

发布评论

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

评论(1

等待我真够勒 2024-12-21 13:40:35

这段代码放错了地方。您不应该在视图控制器的初始化程序中创建和使用视图(假设上述代码位于视图控制器类内部)。

相反,请执行以下操作:

- (id)init
{
    self = [super init];
    if (self) {
        // init any state other than views
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mensaje = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [self.mensaje setText:@"He vuelto"];
    [self.view addSubview:self.mensaje];
    [self.mensaje setHidden:YES];
}

这还假设您正在使用 ARC。如果没有,则需要添加autorelease,如下:

    self.mensaje = [[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)] autorelease];

This code is in the wrong place. You shouldn't be creating and working with views in the initializer of a view controller (assuming the above code is inside a view controller class).

instead, do the following:

- (id)init
{
    self = [super init];
    if (self) {
        // init any state other than views
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mensaje = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)];
    [self.mensaje setText:@"He vuelto"];
    [self.view addSubview:self.mensaje];
    [self.mensaje setHidden:YES];
}

This also assumes you are using ARC. If not, you need to add autorelease as follows:

    self.mensaje = [[[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 100.0, 100.0)] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文