Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 将NSA通知从classA发送到classB_Objective C_Ios_Nsnotificationcenter_Nsnotification - Fatal编程技术网

Objective c 将NSA通知从classA发送到classB

Objective c 将NSA通知从classA发送到classB,objective-c,ios,nsnotificationcenter,nsnotification,Objective C,Ios,Nsnotificationcenter,Nsnotification,因此,我有一个应用程序与应用程序内购买。应用内购买在FirstViewController中进行管理。用户购买产品后,我希望向我的MainTableViewController发送通知,以重新加载表数据并显示在应用内购买中购买的新对象。所以基本上我想从类a向类B发送一个通知,然后类B重新加载tableview的数据。我尝试过使用NSNotificationCenter,但没有成功,但我知道使用NSNotificationCenter是可能的,我只是不知道如何使用。在课堂A:发布通知 [[NSNo

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

在课堂A:发布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
                                                        object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
                                                   object:arrayOfPurchasedObjects];
在B类中:首先注册通知,然后编写一个方法来处理它。
将相应的选择器指定给该方法

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

-(void)handleUpdatedData:(NSNotification *)notification {
    NSLog(@"recieved");
    [self.tableView reloadData];
}

也许你想从另一个线程发送通知?NSNotification不会从另一个线程发送给观察者。

好的,我将在vince的回答中添加更多的信息


A类:发布通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
                                                        object:self];
[[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];
 }

应该这样。运行通知不需要更多。如果classA更新了一些数据,请确保这些更改反映在classB中。您也可以使用通知给classB一个值,但是
reloadData
将使tableView再次向其数据源请求数据。那么,发送通知时不会调用-(void)updated{[self.tableView reloadData];},请检查您的代码,我试过修改代码,但没有用。@Maxner-问题肯定出在别处看看苹果的,你会发现通知系统就像我写的那样简单。好的,还是谢谢你的代码,我将投票支持您和Rahul Vyas的回答。@Maxner-请确保您使用此代码在某个时候有效地注册了通知。您可以通过在之前记录一些消息来轻松检查它。您是否将注册方法调用放入
viewdiload
MainTableViewController
?thx但它也不起作用,而且我使用NSUserdefaults来识别应用内购买是否完成,因此我不需要向数组中添加对象。对于新手(我),上面的回答中有一个输入错误,“postNotificationName”抛出错误,“postNotificationName”(带有另一个“i”)更正错误。(感谢Rahul Vyas帮助澄清通知流程)。您的答案@RahulVyas中有一个输入错误,它不是“postNotificationName”,而是“postNotificationName”。请尝试将生成通知的[NSThread currentThread]与观察通知的类中的[NSThread mainThread]进行比较。同时确保,添加观察者时未指定对象的内容。