我正在尝试观察我的 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