How to access view controller variables from application delegate ... and vice versa?

I would like the view manager to have access to dog in the application delegate.

I want the application delegate to be able to access the mouse in the view controller.


 #import <UIKit/UIKit.h> @interface ViewController : UIViewController { int mouse; // <---------------- } @end 

 - (void)viewDidLoad { [super viewDidLoad]; mouse = 12; // <------------------- NSLog(@"viewDidLoad %d", dog); // <--------------- } 

 #import <UIKit/UIKit.h> @class ViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> { int dog; // <--------------- } @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) ViewController *viewController; @end 

 - (void)applicationWillResignActive:(UIApplication *)application { NSLog(@"applicationWillResignActive %d", mouse); // <-------------- } 

  - (void)applicationDidBecomeActive:(UIApplication *)application { dog = 77; // <--------------------- NSLog(@"applicationDidBecomeActive"); } 
+6
source share
2 answers

Part 1: In ViewController.h:

 -(int)mouse; //add this before the @end 

In ViewController.m add this method:

 -(int)mouse { return mouse; } 

To access the mouse from AppDelegate, use self.viewController.mouse For example:

 NSLog(@"ViewController mouse: %i", self.viewController.mouse); 

Part 2:

In AppDelegate.h:

 -(int)dog; //add this before the @end 

In AppDelegate.m add this method:

 -(int)dog { return dog; } 

In ViewController.m:

 #import "AppDelegate.h" 

To access the dog from ViewController, use this:

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; NSLog(@"dog from AppDelegate: %i", [appDelegate dog]); //etc. 
+9
source

In your view, the controller header file adds the mouse as a property:

 #import <UIKit/UIKit.h> @interface ViewController : UIViewController { NSInteger mouse; // <---------------- } @property (nonatomic, assign) NSInteger mouse; @end 

Synthesize a property in an implementation of your view controller just below the @implementation line:

 @synthesize mouse; 

In its case, the application delegate adds the dog as a property:

 @interface AppDelegate : UIResponder <UIApplicationDelegate> { NSInteger dog; // <--------------- } @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) ViewController *viewController; @property (nonatomic, assign) NSInteger dog; @end 

Also synthesize a dog in its application delegation implementation.

Now in application deletion, assuming that you have a link to a controller of your kind, you can access the mouse as follows:

 viewController.mouse = 13; 

You can do the same with the application delegation class, which can be accessed from any view controller using (assuming the AppDelegate application delegate class name):

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

I would recommend you also use NSInteger instead of int.

+6
source

Source: https://habr.com/ru/post/914826/


All Articles