【问题标题】:NSTimer in AppDelegate does not update UILabel in UIViewControllerAppDelegate 中的 NSTimer 不会更新 UIViewController 中的 UILabel
【发布时间】:2013-05-08 05:03:55
【问题描述】:

我的目标是在应用程序委托中启动一个计时器,并使用它来调用其他视图控制器中的方法。当这些方法被调用时,它们将更新 UILabel 的文本。我正在主线程上执行该方法,但我无法弄清楚为什么 UILabel 没有被更新。我知道正在调用视图控制器中的方法,但 UILabel 没有得到更新。我还在 Interface Builder 中验证了 IBOutlet 连接。我正在使用故事板来布置我的视图并将视图控制器附加到这些视图。我做错了什么?

在我的 AppDelegate.m 中:

#import "AppDelegate.h"
#import "GreyViewController.h"

@interface AppDelegate ()

@property (nonatomic) int counter;
@property (strong, nonatomic) GreyViewController *greyClass;

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSTimer *timer;
    timer = [NSTimer scheduledTimerWithTimeInterval:2.0
                                             target:self
                                           selector:@selector(performOnMain)
                                           userInfo:nil
                                            repeats:YES];

    return YES;
}

- (GreyViewController *)greyClass {
    if (!_greyClass) {
        _greyClass = [[GreyViewController alloc] init];
    }

    return _greyClass;
}

- (void)performOnMain {
    [self performSelectorOnMainThread:@selector(updateLabel) withObject:nil waitUntilDone:YES];
}

- (void)updateLabel {

    self.counter++;
    NSLog(@"appdelegate counter %i", self.counter);

    [self.greyClass updateGreyLabel:[NSString stringWithFormat:@"%i", self.counter]];
}

@end

GreyViewController.h 是:

#import <UIKit/UIKit.h>

@interface GreyViewController : UIViewController

@property (strong, nonatomic) NSString *greyString;
@property (weak, nonatomic) IBOutlet UILabel *greyLabel;

- (void)updateGreyLabel:(NSString *)string;

@end

我的 GreyViewController.m 中的方法:

- (void)updateGreyLabel:(NSString *)string {

    self.greyLabel.text = string;
    NSLog(@"greyviewcontroller string %@", string);
}

【问题讨论】:

  • 在 Appdelegate 中启动计时器之前调用 [self greyClass]; 方法,因为您可能无法访问那里的 GreyViewController 对象,因为它似乎尚未初始化。
  • @DipenPanchasara 在应用程序委托中调用 self greyClass 不起作用
  • 它应该可以工作,没有理由不工作。!!如果你想用self 引用变量,那么更多的事情不要忘记合成它
  • @DipenPanchasara 你不再需要手动合成@property。它由编译器自动为您完成。

标签: objective-c nsstring uilabel nstimer appdelegate


【解决方案1】:

每次调用:

GreyViewController *greyClass = [[GreyViewController alloc] init];

在您的“updateLabel”方法中(即每两秒)您正在实例化一个新的 GreyViewController。您很可能想要这样做。

相反,实例化/创建一次(或在情节提要或 XIB 文件中创建)并更新连接到插座的标签。

【讨论】:

  • 编辑了我的 AppDelegate 中的代码以处理 allocinit。但它仍然不能解决我原来的问题。
  • 这是因为 GreyViewController 中的“greyLabel”属性为 NULL。
  • 但是从updateGreyLabel 中的NSLog 我知道字符串正在使用计时器进行更新。那么为什么相同的字符串不适用于标签呢?
  • 将此行作为“updateGreyLabel”方法的第一行插入:“if(self.greyLabel == NULL) NSLog( @"self.greyLabel is NULL" ) else self.greyLabel.text = string;”。我 95% 确定您没有使用实际的 UILabel 对象设置您的 grayLabel 属性。
  • UILabel 确实为 NULL,我该如何解决?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-22
相关资源
最近更新 更多