Jon*_*asG 4 objective-c nsnotification nsnotificationcenter ios
所以我有一个应用程序购买的应用程序.In App购买在FirstViewController中管理.当用户购买产品时,我想向我的MainTableViewController发送通知以重新加载表数据并显示在In App购买中购买的新对象.所以基本上我想要从A类发送到B类的通知,然后B类重新加载tableview的数据.我曾尝试使用NSNotificationCenter,但没有成功,但我知道它可能与NSNotificationCenter我只是不知道如何.
小智 25
在A类:发布通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:self];
Run Code Online (Sandbox Code Playgroud)
在B类中:首先注册通知,然后编写一个方法来处理它.
您将相应的选择器提供给方法.
// view did load
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleUpdatedData:)
name:@"DataUpdated"
object:nil];
-(void)handleUpdatedData:(NSNotification *)notification {
NSLog(@"recieved");
[self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)
好的,我正在为vince的答案添加更多信息
在A类:发布通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated"
object:arrayOfPurchasedObjects];
Run Code Online (Sandbox Code Playgroud)
在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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11244 次 |
| 最近记录: |