0

I have a NSMutablearray that I want to save to file when the application enters background. I have declared my NSMutablearray in my Viewcontroller.h as an instance variable:

#import <UIKit/UIKit.h>

NSString *docPath();

@interface ViewController : UIViewController<UITableViewDataSource>

{
    UITableView *taskTable;
    UITextField *taskField;
    UIButton *insertButton;

    NSMutableArray *tasks;

}

- (void)addTask:(id)sender;


@end

Now I need to access this variable in my AppDelegate like so:

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [tasks writetofile:docPath() atomically:YES];

}

I am fairly new to this and self taught. I thought some sort of #import "Viewcontroller.h" in the AppDelegate would help, but not sure how to proceed. Any help appreciated.

7
  • Is the view controller the one currently being displayed? If not is there more than once instance of it ever in existence at the same time. Commented Apr 16, 2016 at 12:27
  • Yes the view controller is being displayed and there is only one instance of tasks. Commented Apr 16, 2016 at 12:28
  • You should never access an instance variable of one class from another. Instance variables should always be private. A class should provide an appropriate public interface (methods and properties) that can be access by other classes. Commented Apr 16, 2016 at 14:18
  • I understand, thanks. Looking at all the answers here, I can't help but wonder why is it so easy to access a property of another class, except if that class happens to be a view controller. If you look at the answer by trojanfoe, the amount of code you need when with any other custom class all you'd need is an import statement. Is there any reason why view controllers behave differently? Commented Apr 16, 2016 at 14:41
  • It's trivial to access a property of a view controller. That's not the issue. If your app delegate already had a reference to the view controller it would be one line of code. But there is no reason for your app delegate to know or care about the view controller. The app delegate shouldn't care that a view controller needs to save data. Only the view controller should care. That's why the 1st half of the answer by Surya is the proper solution. Commented Apr 16, 2016 at 14:46

1 Answer 1

1

You can register your viewcontroller for background notification. There is no need for Appdelegate here.

- (void)viewDidLoad{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [tasks writetofile:docPath() atomically:YES];
}

EDIT:

OR you can iterate the navigation stack to get your viewcontroller instance in AppDelegate.

UINavigationController *navigationController = [self.window rootViewController];

for (UIViewController *viewController in navigationController.viewControllers) {
    if (viewController isKindOfClass:[YOUR_CONTROLLER class]) {

        YOUR_CONTROLLER *yourController =  (YOUR_CONTROLLER *)viewController;

        //Do your code using yourController instance
    }
}

NOTE: The above code is based on the assumption that your rootviewcontroller is UINavigationController

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

3 Comments

Ok thanks this is very helpful. But I am curious. How would I import an instance variable of ViewController into AppDelegate?
Just to point out, the first half of this answer is the proper way to solve the actual problem that the OP needs to solve but nothing about this answer actually provides any information about the specific solution being asked for in the question. But the OP is attempting the wrong solution. The app delegate shouldn't be involved in the saving of the data in any way. And it is a really bad idea for any class to try to access the instance variables of any other class.
Note that the first half of this answer is only half complete. Be sure the class calls removeObserver in its dealloc method.

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.