对于旧版本的 xcode/ios,我使用:
appDelegate =(AppDelegate *)[[UIApplication sharedApplication] delegate];
访问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
但是在ios5中AppDelegate从NSObject更改为UIRepsonder
是否可以访问AppDelegate?
欢迎 Ant 评论
最佳答案
不知道你心里有什么误解。但我在iOS 5应用程序开发中使用的仍然是相同的。
根据您上面的评论,您说您编写了这段代码:
#import "AppDelegate.h"
@property (nonatomic,retain) AppDelegate *appDelegate; // This line is unnecessary
您不必为 AppDelegate 类的对象创建属性。只需将此行包含在您要访问全局变量的 .m 文件中即可:
// Create an AppDelegate variable in your MapViewScreen @interface
AppDelegate *appDelegate;
#import "AppDelegate.h"
@implementation MapViewScreen
- (void)viewDidLoad
{
appDelegate = [[UIApplication sharedApplication] delegate];
}
附注:正如迈克尔指出的 UIResponder 继承自 NSObject 所以你不必担心。一切都一样。
关于iphone - 应用程序代理访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10051778/