本文介绍了iPhone,如何使用App Delegate变量,以便可以像全局变量一样使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我正在使用的代码,之后会看到我的错误

Heres the code I'm using, see my error afterwards

@interface MyAppDelegate : NSObject  {
  NSString *userName;
}
@property (nonatomic, retain) NSString *userName;
...
@end

,并在App Delegate的.M文件中编写:

and in the .M file for the App Delegate you would write:

@implementation MyAppDelegate
@synthesize userName;
...
@end

然后,每当您要获取或写入用户名时,您都将编写:

Then, whenever you want to fetch or write userName, you would write:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
someClass.someString = appDelegate.userName;  //..to fetch
appDelegate.userName = ..some NSString..;     //..to write

警告:键入"id"不符合"MyAppDelegate"协议

我的代码中缺少什么?

推荐答案

您应将演员表添加到MyAppDelegate

You should add a cast to MyAppDelegate

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

这篇关于iPhone,如何使用App Delegate变量,以便可以像全局变量一样使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 04:00