如何在 applicationDidBecomeActive 中更新 UILabel

发布于 2024-11-30 06:01:43 字数 631 浏览 1 评论 0原文

我需要通过 applicationDidBecomeActive 更新标签的帮助。我的项目的结构方式是,我有一个 RootViewController。当用户触摸按钮时,会通过 presentModalView 打开一个新的视图控制器。我想在每次应用程序激活时更新此视图控制器上的标签。这是我在应用程序委托中的代码:

WordList1 *viewController1 = [[WordList1 alloc] initWithNibName:@"WordList1" bundle:nil];
[viewController1 changelabel:counter];
[viewController1 release];

它在我的 WordList1 视图控制器中调用此函数以每次更新标签:

  - (void) changelabel:(int)counter {
    NSString *string = [NSString stringWithFormat:@"%d", counter];  
    label.text = string;
}

当我通过调试器运行它时,“计数器”和“字符串”的值正在更新,但标签没有更新改变价值观。请帮忙

I need help with updating a label through in applicationDidBecomeActive. The way my project is structured, I have a RootViewController. When the user touches a button, a new view controller is opened through presentModalView. I want to update the label on this view controller each time the application becomes active. This is my code in my app delegate:

WordList1 *viewController1 = [[WordList1 alloc] initWithNibName:@"WordList1" bundle:nil];
[viewController1 changelabel:counter];
[viewController1 release];

And it calls this function in my WordList1 viewcontroller to update the label each time:

  - (void) changelabel:(int)counter {
    NSString *string = [NSString stringWithFormat:@"%d", counter];  
    label.text = string;
}

When I run it through debugger, the value of "counter" and "string" are updating, but the label is not changing values. Please help

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

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

发布评论

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

评论(2

勿忘心安 2024-12-07 06:01:43

您在哪里创建了 UILabel?如果它是通过您的 xib 文件创建的,请确保您已将文件所有者的 IBOutlet 连接到正确的标签。如果没有连接,它不知道要更新什么标签。

编辑:
我认为从 AppDelegate 更新标签是糟糕的代码设计。标签确实应该从视图控制器更新。我的猜测是,您将代码放在 AppDelegate 中,因为这是应用程序进入前台时调用的代码的位置。要在应用程序进入前台时从视图控制器更新标签,您可以在视图控制器中设置通知。代码示例:

- (id)init
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeLabel:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    return self;
}

这将视图控制器注册为观察者,以便在每次应用程序进入前台时收到通知。这样,您可以在视图控制器中包含此代码,并对其进行相应更新。

另外,请确保在释放视图控制器时取消注册观察者,方法是将以下内容添加到 dealloc 方法中:

[[NSNotificationCenter defaultCenter] removeObserver:self];

Where have you created your UILabel? If it is created through your xib file, make sure you have connected the File Owner's IBOutlet to the correct label. If not connected, it does not know what label to update.

EDIT:
I believe that updating your label from the AppDelegate is bad code design. The label should really be updated from a view controller. My guess is that you are putting the code in the AppDelegate because that's where the code is that is called when the app enters the foreground. To update the label from a view controller when the app enters the foreground, you can set up a notification in the view controller. Code example:

- (id)init
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeLabel:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    return self;
}

This registers the view controller as an observer to be notified each time the app will enter the foreground. This way, you can contain this code in your view controller, and have it be updated accordingly.

Also, make sure you unregister the observer when the view controller is deallocated, by adding the following to your dealloc method:

[[NSNotificationCenter defaultCenter] removeObserver:self];
萝莉病 2024-12-07 06:01:43

在您的 appdelegate 代码中,您从不使用 ViewController1。它没有设置为 rootViewController,也没有推送,也没有什么?我不明白它怎么能显示任何东西?

In your appdelegate code, you never use ViewController1. It's not set as rootViewController, nor pushed nor nothing? I don't understand how it can display anything?

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