如何从应用程序委托访问视图控制器变量...反之亦然?

jdl*_*jdl 6 cocoa-touch objective-c ios

我希望视图控制器能够dog在应用程序委托中访问.

我希望应用程序委托能够mouse在视图控制器中访问.


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    int mouse;  // <----------------
}
@end
Run Code Online (Sandbox Code Playgroud)
- (void)viewDidLoad
{
    [super viewDidLoad];

    mouse = 12;  // <-------------------

    NSLog(@"viewDidLoad %d", dog); // <---------------
}
Run Code Online (Sandbox Code Playgroud)
#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    int dog;  // <---------------
}
@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@end
Run Code Online (Sandbox Code Playgroud)
- (void)applicationWillResignActive:(UIApplication *)application
{
    NSLog(@"applicationWillResignActive %d", mouse); // <--------------
}
Run Code Online (Sandbox Code Playgroud)
 - (void)applicationDidBecomeActive:(UIApplication *)application
{
    dog = 77; // <---------------------

    NSLog(@"applicationDidBecomeActive");
}
Run Code Online (Sandbox Code Playgroud)

use*_*234 8

第1部分:在ViewController.h中:

-(int)mouse;  //add this before the @end
Run Code Online (Sandbox Code Playgroud)

在ViewController.m中,添加以下方法:

-(int)mouse
{
    return mouse;
}
Run Code Online (Sandbox Code Playgroud)

要从AppDelegate访问鼠标,请使用self.viewController.mouse例如;

NSLog(@"ViewController mouse: %i", self.viewController.mouse);
Run Code Online (Sandbox Code Playgroud)

第2部分:

在AppDelegate.h中:

-(int)dog;  //add this before the @end
Run Code Online (Sandbox Code Playgroud)

在AppDelegate.m中,添加以下方法:

-(int)dog
{
    return dog;
}
Run Code Online (Sandbox Code Playgroud)

在ViewController.m中:

#import "AppDelegate.h"
Run Code Online (Sandbox Code Playgroud)

要从ViewController访问dog,请使用以下命令:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"dog from AppDelegate: %i", [appDelegate dog]);  //etc.
Run Code Online (Sandbox Code Playgroud)


Pet*_*sey 6

在视图控制器头文件中,将鼠标添加为属性:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSInteger mouse;  // <----------------
}

@property (nonatomic, assign) NSInteger mouse;

@end
Run Code Online (Sandbox Code Playgroud)

在@implementation行下面的视图控制器实现中合成属性:

@synthesize mouse;
Run Code Online (Sandbox Code Playgroud)

在您的app delegate中添加dog作为属性:

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

@property (strong, nonatomic) ViewController *viewController;

@property (nonatomic, assign) NSInteger dog;

@end
Run Code Online (Sandbox Code Playgroud)

还可以在您的app委托实现中合成dog.

现在在您的app委托中,假设您有对视图控制器的引用,您可以像这样访问鼠标:

viewController.mouse = 13;
Run Code Online (Sandbox Code Playgroud)

您可以使用app delegate类执行相同的操作,可以从任何视图控制器使用(假设您的app delegate类的名称是AppDelegate):

((AppDelegate *)([UIApplication sharedApplication].delegate)).dog = 13;
Run Code Online (Sandbox Code Playgroud)

我建议您也使用NSInteger而不是int.