0

im kinda new to iOS, I'm having a hard time with my array, I'm using NSMutableArray,I have put my array in the AppDelegate, At first load of the app in my viewController the array has no object in it yet, I have a setting that put my integers in the array, but after saving the array was not loaded, but when exited then reOpen, it will load, Could please someone explain how can I solve and why is it like this? Im using it tableview.

in app delegate:

self.integers = [NSMutableArray array];  
for(int i = 0; i <= 10; i++) 
{ .....
  }

loading:

integers = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).integers;

1 Answer 1

1

My answer:

Somewhere in your code you have a loop that loads your integers and puts them into your array, right? You need to put that loop in a method and call it again whenever you want to reset the UITableVIew contents back to its original contents. Then you need to update the array in your AppDelegate and your viewController, so depending which class that method is in, you'll need to copy the integers to the other class using

integers = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).integers;

or

// your array then update
self.integerss = [NSMutableArray array];  

    for(int i = 0; i <= 10; i++) 
    { .....
      }

((AppDelegate *)[[UIApplication sharedApplication] delegate]).integers = self.integers;

Use the same code you used for the array originally to put all the integers back, then call reloadData on the tableview. That will solve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

This will indeed solve your problem but I would also recommend rearranging your design - I can see no good reason why the application delegate should have anything to do with the app data layer and hold an array of integers.
I mean that the AppDelegate class is not meant for whatever you're using it for. Developers tend to make this mistake often because it is the first code that runs. I would recommend moving this data entity to a class that manages data.

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.