r4id4
r4id4

Reputation: 6077

NSNotification sends value that get reset in viewDidLoad

I have this problem: 2 views called A and B, both are TableViewController.

A listen for notification, B sends notification

I'm not going to explain in detail, but for sake of simplicity it's like this:

A can display multiple types of data, B has a list of these types, selecting one row from B makes A load in it's tableView the right list of data.

While i'm in B i'm sending a notification like this

NSNumber *section = [NSNumber numberWithInt:indexPath.row];
NSDictionary *infoDictionary = [NSDictionary dictionaryWithObject:section
                                                               forKey:@"CurrentTableView"]

NSString *UpdateTableView = @"UpdateTableView";
[[NSNotificationCenter defaultCenter] postNotificationName:UpdateTableView
                                                            object:self
                                                          userInfo:infoDictionary];

Now in A i'm listening like this

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateTableView:)
                                                 name:@"UpdateTableView"
                                               object:nil];

which calls the method

@property (strong, nonatomic) NSNumber *section;

// :::: Some code here ::::

- (void)updateTableView:(NSNotification*)note
{
    _section = [[note userInfo] valueForKey:@"CurrentTableView"];
    NSLog(@"%d",[_section intValue]);
    [self.tableView reloadData];
}

and the NSLog works fine, i mean it prints the right value. I'm using _section to discriminate in the TableView delegate methods what kind of data to load.

The problem is that the call to this method (after the notification is received) happens BEFORE the view is actually reloaded (viewDidAppear and so) which set my @property _section to 0, in this way every time my TableView loads the data standing behind the [_section intValue] == 0.

How could i solve this? I need something that don't gets reset every time the view loads itself. Any suggestion?

EDIT: navigation controller to move from B to A

MenuNavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:@"contentController"];

AViewController *homeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"homeController"];
        navigationController.viewControllers = @[homeViewController];

Upvotes: 0

Views: 196

Answers (2)

Devin Pitcher
Devin Pitcher

Reputation: 2762

Perhaps your property section isn't synthesizing to use your instance variable _section. Does your synthesize line look like this:

@synthesize section=_section;

Otherwise the compiler will implicitly create an instance variable name section

UPDATE

As @Justafinger mentioned, as of Xcode 4.4, the ivar will be named _section if you completely leave out the @synthesize line. If you do explicitly synthesize, however, and don't assign the ivar name in the @synthesize line the ivar name will default to section.

Upvotes: 0

streem
streem

Reputation: 9144

Do you really need NSNotification ? Notifications are used where you want to update a view, and by update i mean that the view is already created.

What i would do is fairly simple, in B i would set your NSNumber *section as an ivar (if you display controller A some place different from where you post you notification). When you posted your notification you would just instantiate your ivar.

Then A would have a similar variable and when you want to display A :

AViewController *homeViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"homeController"];
homeViewController.section = section;
navigationController.viewControllers = @[homeViewController];

Upvotes: 0

Related Questions