Pass object from VC to App Delegate

2019-08-19 06:31发布

问题:

I have a few objects in a VC that I want to be accessible from my App Delegate.

My VC launches another app, which does a callback to my app. This callback triggers a method in my App Delegate:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

But I want to access some objects in this method that I had set in that previous VC. Any ideas how?

回答1:

Use

+(id)sharedApplicationDelegate; in delegate.h, and write

+(id)sharedApplicationDelegate{
    return  [[UIApplication sharedApplication] delegate];
}

In delegate.m. Make object data type variable in .h and set its property. Write a method like this:

-(void)setObjectForDelegate:(ObjectType *)value{
//use value obj or set it to other variable
}

How to Use:

#import “XXXXdelegate.h”
[[UIApplication sharedApplication] setObjectForDelegate:object];