从另一个类更新UI

dav*_*ara 1 iphone objective-c ios ios7

我试图分离我的GUI和我的逻辑...例如,我有一个计算某事的类(calc.h/calc.m).我有我的GUI(viewController.h/viewController.m)...现在我想从另一个类更新GUI的一个元素...我已经尝试使用Singleton,如下所示:

calc.m:

viewController *con = [viewController sharedInstance];
con.download.text = @"It Works";

viewController.m:

+ (id)sharedInstance {
static id sharedInstance;
@synchronized(self) {
    if (!sharedInstance)
        sharedInstance = [[viewController alloc] init];
    return sharedInstance;
}
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用......

更新:

在我的ViewController中:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(receiveNotification:)
                                             name:@"UpdateDownloadLabel"
                                           object:nil];

- (void) receiveNotification:(NSNotification *) notification
{

NSDictionary* userInfo = notification.userInfo;

if ([[notification name] isEqualToString:@"Update"])
{
    download.text = [userInfo valueForKey:@"Test"];
}
}
Run Code Online (Sandbox Code Playgroud)

在我的其他课程中,我发布了这样的通知:

[[NSNotificationCenter defaultCenter]
 postNotificationName:@"UpdateDownloadLabel"
 object:nil userInfo:info];
Run Code Online (Sandbox Code Playgroud)

但它不起作用......

and*_*azz 6

这不是单身人士的正确使用.如果您需要从模型中通知您的UI,您可以选择以下选项:
- 委托
- 本地通知
- KVO
一般规则是将您的逻辑与您的演示文稿分离,因此您的calc类不应该真正知道名称download存在的标签.