I have a tabbar controller with 2 tabs - A and B. Tab A is a regular UIViewController and tab B is a TableViewController. I am trying to post a NSNotification from tab A and receive the the same and display the data in the table in tab B.
I post the notification from Tab A as below:
//"itemAddedToCartDictionary" is the object that I am sending with notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"ItemAddedToCart" object:nil userInfo:itemAddedToCartDictionary];
In my tab B(TableViewController), I am trying to receive the above notification update an NSMutableArray property. The property is declared as below:
Tab B - .h file:
@property (nonatomic,strong) NSMutableArray *cart;
Tab B - .m file:
//providing manual setter method for 'items' hence not using @synthesize
- (void)setCart:(NSMutableArray *)cart{
_cart = cart;
[self.tableView reloadData];
}
Now, I have placed the code for receiving the notification(in Tab B) in AwakeFromNib as below:
- (void)awakeFromNib{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCurrentItemToCartFromNotification:) name:@"ItemAddedToCart" object:nil];
}
The code, calls the method "addCurrentItemToCartFromNotification" upon receipt of the notification which the updates my property:
- (void)addCurrentItemToCartFromNotification:(NSNotification *)notification{
NSDictionary *currentItem = [notification.userInfo objectForKey:@"CART_ITEM_INFORMATION"];
if (!self.cart){
NSLog(@"self.cart == nil");
self.cart = [[NSMutableArray alloc] init];
}else{
NSLog(@"self.cart != nil");
}
[self.cart addObject:currentItem];
}
Now this is the problem that I am facing:
After I post the notification in Tab A, Tab B(TableViewController) does not show any data even though I have updated my property in the above methods form the received notification. My TableView datasource methods are as below:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.cart count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"itemInCart";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
NSDictionary *cartItem = [self.cart objectAtIndex:indexPath.row];
cell.textLabel.text = [cartItem objectForKey:@"ITEM_NAME"];
return cell;
}
So Basically, I am accessing the property of my TableViewController(that I am receiving and updating from a Notification) from the datasource methods and it is not returning data.
Could you please let me know where and what I am missing here.
Thanks, Mike
EDIT: Following response from @Joel, @martin, @mkirci
Adding reload data to my "addCurrentItemToCartFromNotification" (method being called upon receipt of notification) helped. I a now able to see the items received fomr the notification on my Tab B(TableViewController).
Now here's what is happening:
Whenever a notification is received, the NSMutableArray property is being returned as nil. Hence, every time a notification is received, the alloc init happens for the NSMutableArray property (on the addCurrentItemToCartFromNotification) - (instead of just the first time). Hence, instead of incrementing the array with objects received from the notification, the array is re created every time and only the object form the current notification is being added.
Could you please throw some light on this situation please. Appreciate all your responses.
Thanks, Mike
EDIT2
Updated code snipped for initwithnibname for suggestion from @Joel
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
if (!self.cart){
NSLog(@"self.cart == nil");
self.cart = [[NSMutableArray alloc] init];
}else{
NSLog(@"self.cart != nil");
}
return self;
}