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)
但它不起作用......