2012-04-07 58 views
4

对于旧版本的Xcode/IOS,我用:的AppDelegate访问

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 

但是从NSObject的iOS5中的AppDelegate变化UIRepsonder

是否有可能访问AppDelegate?

欢迎蚂蚁评论

回答

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]; 
} 

附::正如Michael指出的UIResponder继承NSObject所以你不必担心。一切都是一样的。

+0

它工作?或仍然出现错误? – 2012-04-07 05:19:04

0

是的,你可以使用你正在使用访问的appDelegate同样的事情..

+0

我写道: #进口 “AppDelegate.h” @属性(非原子,保留)的AppDelegate *的appDelegate; – arachide 2012-04-07 04:47:49

+2

但它报告错误:未知类型AppDelegate – arachide 2012-04-07 04:48:24

相关问题