1

I have seen many many answers about this but for some reason it is not working for me. I have a property in file A, then in file B I set the variable to some value an on the viewdidappear on file A I /Run some code" based on what the variable was assigned to.

So. this is my code. (bare minimal) ViewController.H

@property(strong, nonatomic) NSString *option;

ViewController.M

-(void)viewDidAppear:(BOOL)animated
{
    NSLog(@"Has apeparsed");
    if ([_option isEqualToString:@"1"]) {

            [_map removeAnnotation:budAnno];
    }
}

ViewControllerB.h

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[recipes objectAtIndex:indexPath.row] isEqualToString:@"TEST"])
    {
        ViewController *viewO =[[ViewController alloc]init];
        viewO.option=@"1";
    }
}

when this code is called in ViewControllerB Options is set to 1. but when I click BACK to viewContoller.M my if statement fails because it is back to nil. would like to know why this is done.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Liberace
  • 81
  • 1
  • 11
  • 1
    Hi, please see my answer here (http://stackoverflow.com/questions/24312447/calling-method-from-another-view-controller-not-working-c-objective-xcode/24315305#24315305)... Identical case. – danh Dec 21 '14 at 01:39
  • @danh Thanks, I went back to it and it helped me – Liberace Dec 21 '14 at 03:14

1 Answers1

3

The problem is that you are setting options to 1 on a NEW instance of view controller A, not the original one. It's a new instance because you are doing alloc/init.

When you go back, you are going back to the ORIGINAL instance of your view controller A. You have not changed its options value.

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58