AppDelegate Access

时间:2012-04-07 04:34:08

标签: iphone

对于旧版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?

欢迎蚂蚁评论

3 个答案:

答案 0 :(得分:9)

我不知道你心中有什么误解。但我在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];
}

P.S。:正如Michael指出 UIResponder 继承自 NSObject 所以您不必担心。一切都是一样的。

答案 1 :(得分:4)

您不必为iOS 5更改任何内容。UIResponder class继承自NSObject。

答案 2 :(得分:0)

是的,您可以使用您用来访问appDelegate的相同内容..

相关问题