因此,我有一个具有应用程序购买的应用程序。在App购买中,在FirstViewController中管理。用户购买该产品后,我想向我的VaintableViewController发送通知以重新加载表数据并显示在App购买中购买的新对象。因此,基本上,我想从A类发送通知到B类,然后B类重新加载TableView的数据。我尝试使用nsnotificationcenter,但没有成功,但是我知道nsnotificationcenter的可能性可能是可能的。

有帮助吗?

解决方案

在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];
}

其他提示

好吧,我在文斯的答案中添加了更多信息

在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];
 }

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top