当应用程序激活时更新uilabels [英] update uilabels when application becomes active

查看:47
本文介绍了当应用程序激活时更新uilabels的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个非常简单的事情:当应用程序从后台运行后变为活动状态时,请在(唯一的)视图控制器上更新一些标签(例如,为简化起见,请使用当前时间(即使在就我而言,我正在更新电话参数,例如通话状态等,但这没关系).

I am trying to do something very simple: update a few labels on my (one and only) view controller when the application becomes active after coming from the background (say with the current time for the simplicity of it (even though in my case, I am updating telephony parameters, such as call states, etc., but that should not matter)).

老实说,我确定一旦调用 viewDidLoad ,此方法中的代码就会再次运行,因此我的所有标签都将是最新的.但这不会发生,除非我终止了该应用程序并重新启动它,所以我在 applicationDidBecomeActive 下的应用程序委托中执行的操作,我将其称为视图控制器方法:

Honestly, I was sure that once I call viewDidLoad, the code in this method runs again and so all my labels will be up to date. But that doesnt happen, unless I kill the app and relaunch it, so what I did in the app delegate under applicationDidBecomeActive, I called my view controller method:

ViewController *vc = [[ViewController alloc] init];
[vc refreshCalls];

哪个会执行此代码,但是标签什么也没有发生.这样做时,我还会显示一个 UIAlertView ,它确实具有更新的信息,但没有标签.

Which does execute this code, but nothing happens to the labels. When doing this, I am also showing an UIAlertView that DOES have the updated info on it, but not the labels.

我该如何解决?任何想法将不胜感激.

How can I fix this? Any ideas would be very appreciated.

这是我的应用程序委托applicationDidBecomeActive方法:

Here is my app delegate applicationDidBecomeActive method:

- (void)applicationDidBecomeActive:(UIApplication *)application
{

ViewController *vc = [[ViewController alloc] init];
[vc refreshCalls];
}

以及视图控制器中的 refreshCalls 方法:

And the refreshCalls method in the view controller:

 -(void) refreshCalls {

NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(@"User's current time in their preference format:%@",currentTime);

  UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"TIME" message:currentTime delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil , nil];

 [alert1 show]; --> does show an update time        

myLabel.text = currentTime;
[self.view addSubview:myLabel]; --> nothing happens, stays the last time

 }

推荐答案

首先,您不应该调用 ViewDidLoad .它会为您调用,就像 applicationDidBecomeActive .

First of all, you shouldn't call ViewDidLoad. It gets called for you, just like applicationDidBecomeActive.

现在,回答:

正在发生的事情是,您正在新的 ViewController 上调用您的 refreshCalls 方法,而不是可能在应用程序委托中某个位置创建的现有方法.首次创建 ViewController 时,您可能已将其视图添加到 UIWindow .

What is happening is that you are calling your refreshCalls method on a new ViewController instead of your existing one, which you probably created somewhere in your app delegate. When you first created your ViewController, you probably added its view to UIWindow.

然后,当您在 applicationDidBecomeActive 中创建新的 ViewController 并对其调用 refreshCalls 时,它会像应有的那样进行更新,但是视图没有显示在屏幕上,因为您没有将其添加到 UIWindow .

Then, when you create a new ViewController in applicationDidBecomeActive and call refreshCalls on it, it gets updated just like it should, but its view is not shown on the screen, because you didn't add it to UIWindow.

尝试在您的 appDelegate 中找到您首先在何处创建 ViewController 并将其分配给属性的位置.然后,不要在 applicationDidBecomeActive 中创建新的 ViewController ,而要获取现有的并在其上调用 refreshCalls 方法.

Try to locate in your appDelegate where do you first create your ViewController and assign it to a property. Then, instead of creating a new ViewController in applicationDidBecomeActive, get your existing one and call your refreshCalls method on it.

这篇关于当应用程序激活时更新uilabels的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆