How to refer to an AppDelegate instance variable from a custom controller action?

I want to be able (as an exercise for training for controllers) to have a button in the initial view that I configure, then if you click on the button, an action will take place that changes the view from view 1 to view 2. I don’t know how it you? See the section in the code below where I would like to introduce code that does this.

Can I post the code I need? (i.e. will cover how to refer to the variable I set in AppDelegate). Let me know if this exercise I gave myself is ruined somewhere. Thank.

AppDelegate * .h

#import <UIKit/UIKit.h>
@class gregsController;
@class Gregs2ndController;  

@interface windowsBasedAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    gregsController *viewController;
    Gregs2ndController *view2Controller;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet gregsController *viewController;
@property (nonatomic, retain) IBOutlet Gregs2ndController *view2Controller;

@end

AppDelegate * .m

#import "windowsBasedAppDelegate.h"
#import "gregsController.h"
#import "Gregs2ndController.h"

@implementation windowsBasedAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize view2Controller;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.
    NSLog(@"windowsBasedAppDelegate - didFinishLaunchingWithOptions");

    //[self.window addSubview:viewController.view];
    [self.window addSubview:view2Controller.view];
    [self.window makeKeyAndVisible];

    return YES;
}

User controller * .m

#import "gregsController.h"
@implementation gregsController
- (IBAction)logSomething {
    NSLog(@"About to switch views");
    [self.view removeFromSuperview];

        // *** HOW DO I REFERENCE view2Controller AND PUT THIS AS THE VIEW  ???? ****

    NSLog(@"Finished switching views");
}
+3
source share
3

, , UIApplication singleton:

#import "gregsController.h"
@implementation gregsController
- (IBAction)logSomething {
    NSLog(@"About to switch views");
    [self.view removeFromSuperview];

    // *** HOW DO I REFERENCE view2Controller AND PUT THIS AS THE VIEW  ???? ****
    // Like this:
    windowsBasedAppDelegate* appDelegate = (windowsBasedAppDelegate*)[[UIApplication sharedApplication] delegate];
    [appDelegate.window addSubview:appDelegate.view2Controller.view];

    NSLog(@"Finished switching views");
}
+7

,

testAppDelegate * appDelegate = (testAppDelegate *) [[UIApplication sharedApplication] delegate];

, ,

appDelegate.view2Controller

+1

You can access the application delegate and change its appearance as follows:

#import "windowsBasedAppDelegate.h"

windowsBasedAppDelegate *delegate = (windowsBasedAppDelegate *) [[UIApplication sharedApplication] delegate];
view2Controller *view_controller = delegate.view2Controller;

[window addSubView:view_controller.view];
[window becomeKeyWindow];
+1
source

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


All Articles