应用程序激活时更新uilabels
我试图做一些非常简单的事情:当应用程序从后台变为活动状态时,更新我的(一个且唯一的)视图控制器上的一些标签(用当前的时间来说明它的简单性(即使在我的情况下,我正在更新电话参数,如呼叫状态等,但这应该不重要))。
老实说,我确信,一旦我调用viewDidLoad
,这个方法中的代码再次运行,所以我所有的标签将是最新的。 但是这没有发生,除非我杀了应用程序并重新启动它,所以我在applicationDidBecomeActive
的应用程序委托中做了什么,我调用了我的视图控制器方法:
ViewController *vc = [[ViewController alloc] init]; [vc refreshCalls];
哪个执行此代码,但标签没有任何反应。 当这样做的时候,我也显示一个UIAlertView
,它有更新的信息,但不是标签。
我怎样才能解决这个问题? 任何想法将非常感激。
这里是我的应用程序委托applicationDidBecomeActive方法:
- (void)applicationDidBecomeActive:(UIApplication *)application { ViewController *vc = [[ViewController alloc] init]; [vc refreshCalls]; }
以及视图控制器中的refreshCalls
方法:
-(void) refreshCalls { NSDate *today = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // display in 12HR/24HR (ie 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
一样。
现在,来回答:
现在发生的事情是,你正在一个新的ViewController
上调用你的refreshCalls
方法,而不是你现有的,你可能在你的应用程序委托中创build的。 当你第一次创build你的ViewController
,你可能将其视图添加到了UIWindow
。
然后,当您在applicationDidBecomeActive
创build一个新的ViewController
并调用refreshCalls
时,它会像应该更新一样,但是它的视图不会显示在屏幕上,因为您没有将它添加到UIWindow
。
尝试在你的appDelegate
中find你在哪里首先创build你的ViewController
并将其分配给一个属性。 然后,不是在applicationDidBecomeActive
中创build一个新的ViewController
,而是获取你现有的ViewController
并调用你的refreshCalls
方法。
我认为如果你已经有一个myLabel
在你看来,你可能不应该添加另一个。 也许你可以尝试拥有一个self.myLabel
像self.myLabel
,只是调用self.myLabel.text = currentTime;
每次你想更新文本。 (没有addSubview
再次或东西..)
你可以更新你的ViewController
的viewDidAppear:animated
标签viewDidAppear:animated
而不是使用applicationDidBecomeActive
。 像这样的东西:
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [self refreshCalls]; }
为了避免混淆,我决定创build另一个答案作为我以前的结果。
基本的想法是保持你的UIApplication
类中的订阅者列表,并在收到UIApplicationDelegate
事件时通知他们。 这是如何做到这一点。
步骤1
在您的UIApplication
派生类头中添加静态方法的定义来pipe理列表:
+ (void)addDelegate:(id <UIApplicationDelegate>)delegate; + (void)removeDelegate:(id <UIApplicationDelegate>)delegate;
我使用的是UIApplicationDelegate
因为它是一个“自然”的解决scheme。 但是你可以定义你自己的协议或者扩展UIApplicationDelegate
。
第2步
在您的应用程序类中添加实现。 首先在最后一个#import
和@implementation
之间添加列表的定义。
static NSMutableArray *_delegates;
在@implementation
里面我们添加了静态方法体和初始化:
+ (void)initialize { _delegates = [[NSMutableArray alloc] init]; } + (void)addDelegate:(id <UIApplicationDelegate>)delegate { [_delegates addObject:delegate]; } + (void)removeDelegate:(id <UIApplicationDelegate>)delegate { [_delegates removeObject:delegate]; }
对于每个需要通知订阅者的UIApplicationDelegate
方法,添加类似于此示例的代码:
- (void)applicationDidBecomeActive:(UIApplication *)application { NSEnumerator *enumerator = [_delegates objectEnumerator]; id delegate; while (delegate = [enumerator nextObject]) { if ([delegate respondsToSelector:@selector(applicationDidBecomeActive:)]) { [delegate applicationDidBecomeActive:application]; } } }
第3步
在您的UIViewController
(或任何其他类)添加代码来订阅,取消订阅和处理通知。 不要忘记导入你的UIApplication
类头。
订阅可以在你的init方法里面设置,或者在viewDidLoad
里面的UIViewController
也可以。
[MyAppDelegate addDelegate:self]; // your class should implement UIApplicationDelegate
不要忘记取消订阅或不好的事情可能发生! 在你的dealloc方法里添加这个,或者在viewDidUnload
里面添加UIViewController
。
[MyAppDelegate removeDelegate:self];
最后添加你需要得到的有关ex的通知的UIApplicationDelegate
方法:
- (void)applicationDidBecomeActive:(UIApplication *)application { // add your notification handling code here }