-2

I am trying to call the method addObjectToArray from SecondViewController.m. The NSLog works, however I cannot add "foo" to _myArray (an NSMutableArray that is the data source for the UITableView). If I call [self addObjectToArray] in viewDidLoad, then it works fine.

FirstViewController.m

-(void)addObjectToArray {
    [_myArray addObject:@"foo"];
    [_myTableView reloadData];
    NSLog(@"it works");
}

SecondViewController.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"identifier"]) {
        FirstViewController *controller = [segue destinationViewController];
        [controller addObjectToArray];
    }
}
  • 1
    you are calling the method addObjectToArray on an Instance of FirstViewController named myObject but you are presenting firstViewController? and I assume you are using storyboards so why you are initializing myObject by using new ? – Nofel Mahmood Sep 28 '14 at 21:15
  • How should I initialize it? (I am pretty new to Objective C) – user3578956 Sep 28 '14 at 21:17
  • by using [self.storyboard instantiateViewControllerWithIdentifier:@"identifierNameHere"]; – Nofel Mahmood Sep 28 '14 at 21:18
  • I'm sorry I don't understand. I am using storyboards but I needed to push to a view controller when the button pressed and I didn't want to use a segue – user3578956 Sep 28 '14 at 21:23
  • You're making a mistake that strongly suggests you do not really understand the concept of "objects". – Hot Licks Sep 28 '14 at 22:11

1 Answers1

1

As per Nofel Mahmood's comment, you are creating two separate instances of FirstViewController, one named firstViewController (created from the storyboard) and one called myObject (created with the 'new' method). You then call the addObjectToArray method on myObject, but you present firstViewController. Your myObject is essentially redundant. Amend your code as follows:

-(IBAction) doSomething:(id)sender {
FirstViewController *firstViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"firstViewController"];

[firstViewController addObjectToArray];

[self presentViewController:firstViewController animated:YES completion:nil];  

}

EDIT

Since you want to return to an existing instance of FirstViewController, you need to use an unwind segue. There's a detailed explanation here: what-are-unwind-segues, but in your case:

In FirstViewController, add a new method:

- (IBAction)unwindToFirst:(UIStoryboardSegue *)unwindSegue {
    [self addObjectToArray];
}

Then in your storyboard, ctrl-drag from the SecondViewController (or if you prefer, from a specific control in the view) to the green "Exit" icon in the bar below the SecondViewController. You should then select the unwindToFirst action in the small popup that appears.

If you want to use this unwind segue from code, look in the Document Outline on the left hand side of your storyboard for the Unwind Segue you just created. Select this, and then add an identifier in the attributes inspector on the right hand side. You can then call this segue from code using the normal [self performSequeWithIdentifier:...] method.

Community
  • 1
  • 1
pbasdf
  • 20,407
  • 3
  • 36
  • 65
  • I've done this, but it still doesn't add anything to the array – user3578956 Sep 30 '14 at 09:28
  • Does the NSLog in addObjectToArray work? If so, try using `NSLog(@"Array is %@",_myArray];` to see what is actually in the array. – pbasdf Sep 30 '14 at 09:42
  • Also, in which FirstViewController method do you create the _myArray instance? – pbasdf Sep 30 '14 at 09:44
  • for some reason the array is null, even though I add an object in viewDidLoad in FirstViewController.m – user3578956 Sep 30 '14 at 09:47
  • Therein lies the problem - viewDidLoad has not run at the point your addObjectToArray runs. I would override `initWithCoder:`, which is called when the storyboard instantiateViewController method runs, and create your _myArray in that method. – pbasdf Sep 30 '14 at 09:51
  • The structure of my app is: you go to FirstViewController (with the table view, and with myArray as the datasource), then to SecondViewController to add an object to the array, then back to FirstViewController to see the newly added object in the table view. – user3578956 Sep 30 '14 at 10:00
  • Sorry, misunderstood - you should not be using `instantiateViewController...`. That is creating a new, completely separate instance of FirstViewController. And you should not be presenting that new instance. You should be using `dismissViewController` to return to your existing instance. Check out this [link](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) for help on transferring data between viewControllers. – pbasdf Sep 30 '14 at 10:11
  • I got rid of `instantiateViewController` and am now using `prepareForSegue` but the array is still null – user3578956 Sep 30 '14 at 19:46
  • How do you create and present the secondViewController? Can you update your question to show. Also your current prepareForSegue code to get back to the First view controller. – pbasdf Sep 30 '14 at 19:54
  • SecondViewController is created and presented in the storyboard with a modal segue – user3578956 Sep 30 '14 at 20:08
  • Sorry, but your segue from secondViewController back to first has the same problem - it just creates a new version of FirstViewController, it doesn't return to the existing version. What you need to use is an "unwind segue". I'll amend my answer to explain. – pbasdf Sep 30 '14 at 20:59
  • Thanks! It's now working, however when navigate from FirstViewController to my base ViewController (connected via a modal segue), the UITableView is empty – user3578956 Oct 01 '14 at 13:02
  • Happy to help, but can I suggest you begin a new question, setting out your view controller hierarchy, how you segue between them, which one has the tableView, what the data source and delegates are, etc – pbasdf Oct 01 '14 at 15:09