0

I have an int pageNumber setup like so @property (nonatomic, assign) int pageNumber; which I am trying to pass between two view controllers.

It works the first time I tap a cell in the tableview, however if I tap a cell again the int does not get updated.

Any ideas? My code is below.

LeftMenuViewController.m

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone"
                                                             bundle: nil];

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    SoftwareCheckViewController *sc = [[SoftwareCheckViewController alloc] init];

    if ([cell.textLabel.text  isEqual: @"S:1 R:0"]) {

        sc = [mainStoryboard instantiateViewControllerWithIdentifier: @"SoftwareCheckViewController"];
        sc.pageNumber = 1;

    }
    else if ([cell.textLabel.text  isEqual: @"S:1 R:1"]) {

        sc = [mainStoryboard instantiateViewControllerWithIdentifier: @"SoftwareCheckViewController"];
        sc.pageNumber = 2;

    }
    else if ([cell.textLabel.text  isEqual: @"S:1 R:2"]) {

        sc = [mainStoryboard instantiateViewControllerWithIdentifier: @"SoftwareCheckViewController"];
        sc.pageNumber = 3;

    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pageNumberNotification" object:self];
}

SoftwareCheckViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

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

}


- (void)pageNumberMethod{

    NSLog(@"pageNumber: %i", pageNumber);

}
Sami
  • 1,374
  • 1
  • 16
  • 43

1 Answers1

0

You are creating new view controllers every time you tap a cell but you aren't doing anything with them. Setting sc.pageNumber is never going to have an effect if sc isn't the controller that's listening for a notification.

Also, even if you wanted a new controller, [[SoftwareCheckViewController alloc] init] is basically a waste because you then assign something different to sc right after.

Phillip Mills
  • 30,888
  • 4
  • 42
  • 57
  • Thanks, I think I understand, any ideas on how I resolve this? – Sami Jul 03 '15 at 18:38
  • You could possibly create your `LeftMenuViewController` with a reference to the existing `SoftwareCheckViewController` if they are related, or are related to anything in common. Or, you could create a `NSNumber` for the page you want and make it part of the notification (object or userInfo) where the receiver can extract it. You could even make it a property of `LeftMenuViewController`, which you're currently passing as the notification object. – Phillip Mills Jul 03 '15 at 19:08