0

I am having problems working with an NSMutableArray in my program.

There is an array contained within my view controller that has strings added throughout the course of the program. The array is declared within the viewController.

NSMutableArray *tableData;

I am trying to run a method within the same view controller to save the array elements to a table. When the method is accessed through the view controller as shown below it works correctly

[self saveData];

When I call the same saveData method through the app delegate the array does not seem to contain any data.

listView is the object within the appDelegate to reference the view controller containing the save method.

ListAppViewController* listView = [[ListAppViewController alloc] init];
[listView saveData];

This method is being called from the applicationWillTerminate method. I'm thinking the array elements are not available to the appDelegate and that's why the count is 0?

6
  • 2
    Where do you initialize the mutable array? [[NSMutableArray alloc]init] Commented Nov 16, 2013 at 0:26
  • How do you obtain listView from AppDelegate? Commented Nov 16, 2013 at 0:27
  • This is a memory management error.. Commented Nov 16, 2013 at 0:32
  • "I'm thinking the array elements are not available to the appDelegate and that's why the count is 0?" The array elements are available anywhere that the address is available. How do you pass the address to the app delegate? Commented Nov 16, 2013 at 0:43
  • 1
    Look at it this way. You're creating two different instances of ListAppViewController. They look identical but are distinct "boxes". Anything you put into one box will not magically appear in the other box. Commented Nov 16, 2013 at 0:53

2 Answers 2

2

When you do ListAppViewController* listView = [[ListAppViewController alloc] init], you create a new instance of the view controller. If the intention is to save any data you had in that view controller, this won't work (as you're not accessing the instance your app has been using).

Here's how you can do what you want:

  1. Post a notification from your app delegate's applicationWillTerminate method
  2. Subscribe to that notification in ListAppViewControllerList
  3. Call [self saveData] in the notification handler.
Sign up to request clarification or add additional context in comments.

Comments

2

applicationWillTerminate is basically never called. You need to trigger the save when the app goes to the background (resigns active).

1 Comment

The terminate seems to run as expected when the application is closed down. The issue is that it cannot access the array properly it seems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.