简体   繁体   中英

AppDelegate Access

For the old version xcode/ios, I used:

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

to access AppDelegate

#import "AppDelegate.h"


 @property (nonatomic,retain) AppDelegate *appDelegate;



#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

}
@property (strong, nonatomic) UIWindow *window;


@end

//-----

#import <UIKit/UIKit.h>

#import "AppDelegate.h"
@interface DetailController : UIViewController{
}
  @property (nonatomic,retain) AppDelegate *appDelegate;



@end

but in ios5 AppDelegate change from NSObject to UIRepsonder

Is it possible to access the AppDelegate?

Welcome ant comment

I do not know what misconception you have in your mind. But What I use in iOS 5 application development is still same.

As per your comment above you said you wrote this code:

#import "AppDelegate.h"
@property (nonatomic,retain) AppDelegate *appDelegate;  // This line is unnecessary

You do not have to create property for AppDelegate class's object. Just include this line in your.m file where you want to access global variable:

// Create an AppDelegate variable in your MapViewScreen @interface
AppDelegate *appDelegate;

#import "AppDelegate.h"
@implementation MapViewScreen

- (void)viewDidLoad  
{
    appDelegate = [[UIApplication sharedApplication] delegate];
}

PS : As Michael pointed out UIResponder inherits from NSObject so you do not have to worry. Everything's the same.

You don't have to change anything for iOS 5. The UIResponder class inherits from NSObject.

Yes you can use the same thing you were using to access appDelegate..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM