【发布时间】:2013-01-18 20:21:16
【问题描述】:
一段时间以来,我一直在尝试通过从 AppDelegate.m 到 MainViewController.m 的方法调用来更新 UILabel。我真的不明白为什么这不起作用。 方法被调用了并且一切正常除了最后一点更改/更新标签文本。
工作流程
在 AppDelegate 中的 applicationDidBecomeActive 中调用 MainViewController 中的方法 updateLabelMethod 处理数据并更新标签。
代码
MainViewController.h
UILabel *daysResultOutlet;
@interface MainViewController : UIViewController <FlipsideViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *daysResultOutlet;
@end
@interface MainViewController ()
- (void) updateLabelMethod;
@end
MainViewController.m
@synthesize daysResultOutlet;
- (void) updateLabelMethod {
NSString *value = @"test";
NSLog(@"Testing to print value: %@",value);
[daysResultOutlet setText:value]; //insert in label
}
AppDelegate.m
#import "AppDelegate.h"
#import "MainViewController.h"
@interface MainViewController ()
@end
- (void)applicationDidBecomeActive:(UIApplication *)application
{
MainViewController *mvsAsObj = [[MainViewController alloc] init];
[mvsAsObj updateLabelMethod]; //running function, value correctly logged but lbl not updated
mvsAsObj.daysResultOutlet.text = @"update!!"; // not working!
}
结果与尝试
标签不会通过跨类方法调用 updateLabelMethod 或通过 mvsAsObj.daysResultOutlet.text = @"update!!"; 更新,但是,该方法被调用并且它的值是:LOG: Testing to print value: test。此外,如果我从 MainViewController 中调用此方法:[self updateLabelMethod] 一切正常。
我已经尝试了基本上所有的解决方案,但问题是,我在这里做的是直接解决几个 Stackoverflow 问题,所以我不知道如何继续。我正在使用故事板。
还有什么想法?
【问题讨论】:
-
这与 Xcode 没有任何关系。
标签: ios objective-c class uilabel