5

所以我有一个应用程序内购买的应用程序。In App 购买在 FirstViewController 中进行管理。当用户购买了产品后,我想向我的 MainTableViewController 发送通知以重新加载表格数据并显示在应用内购买中购买的新对象。所以基本上我想从 A 类向 B 类发送通知,然后 B 类重新加载 tableview 的数据。我曾尝试使用 NSNotificationCenter,但没有成功,但我知道它可能与 NSNotificationCenter 一起使用,我只是不知道如何。

4

3 回答 3

27

在 A 类中:发布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
                                                        object:self];

在 B 类中:首先注册通知,并编写一个方法来处理它。
您为方法提供相应的选择器。

// view did load
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleUpdatedData:)
                                             name:@"DataUpdated"
                                           object:nil];

-(void)handleUpdatedData:(NSNotification *)notification {
    NSLog(@"recieved");
    [self.tableView reloadData];
}
于 2011-07-25T11:29:28.593 回答
8

好的,我正在为文斯的答案添加更多信息

在 A 类中:发布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
                                                   object:arrayOfPurchasedObjects];

在 B 类中:首先注册通知,并编写一个方法来处理它。
您为方法提供相应的选择器。在发布通知之前确保您的 B 类已分配,否则通知将不起作用。

- (void) viewDidLoad {
// view did load
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleUpdatedData:)
                                             name:@"DataUpdated"
                                           object:nil];
}

-(void)handleUpdatedData:(NSNotification *)notification {
    NSLog(@"recieved");
    NSArray *purchased = [notification object];
    [classBTableDataSourceArray addObjectsFromArray:purchased];
    [self.tableView reloadData];
}

- (void) dealloc {
    // view did load
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                 name:@"DataUpdated"
                                               object:nil];
    [super dealloc];
 }
于 2011-07-25T12:14:33.867 回答
0

也许您试图从另一个线程发送通知?NSNotification 不会从另一个线程传递给观察者。

于 2011-07-25T12:09:38.673 回答