【发布时间】:2013-07-15 12:57:02
【问题描述】:
我正在尝试观察我的 AppDelegate 的属性以更新表格视图。这有点复杂,所以这是我的一些代码。
我想在数组更新时更新 UITableView 的内容。我觉得有一种更有效的方法可以做到这一点,但似乎无法弄清楚。我已经在线阅读了 Apple 的文档,有点困惑。提前谢谢!! :)
//Game.h
@interface Game: NSObject
@property (strong,nonatomic) NSMutableArray *myArray;
@end
//AppDelegate.h
#import "Game.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong,nonatomic) Game *myGame;
@end
//ViewController.m
#import "AppDelegate.h"
@implementation ViewController
//...
- (void)viewDidLoad
{
[(AppDelegate*)[[UIApplication sharedApplication] delegate] addObserver:self forKeyPath:@"myGame" options:0 context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//THIS METHOD NEVER GETS CALLED
NSLog(@"change observed");
[self.tableView reloadData];
}
- (void)dealloc
{
[(AppDelegate*)[[UIApplication sharedApplication] delegate] removeObserver:self forKeyPath:@"myGame"];
}
//...
@end
【问题讨论】:
-
尝试使用
myGame.myArray的关键路径。
标签: ios objective-c observers addobserver