嗨,我看到了这个问题的答案:
How to pass a value from one view to another view
我遇到了一些麻烦。我在 AppDelegate 的头文件中存储了一个字符串。
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
NSString *commString;
}
@property (strong, nonatomic) UIWindow *window;
@end
我现在需要访问它并在一个 View 中更改它。然后在另一个 View 中显示它。上一页的答案简要解释了这一点,但我对答案的第二部分有疑问。它不会让我这样做:
AppDelegate.commString = myString; //mystring being an NSString
有什么想法吗?
谢谢
最佳答案
问题是双重的。首先,您正在尝试访问类上的 ivar,其次,它是一个类而不是实例。 [[UIApplication sharedApplication] delegate];
以单例形式返回委托(delegate)类的有效实例,以便在多个位置轻松访问,但您需要将 ivar 声明为 @属性(property),否则冒着使用(极其不稳定的)结构访问运算符的风险。
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) NSString *commString; //@synthesize this
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate *del = [[UIApplication sharedApplication] delegate];
del.commString = myString;
关于objective-c - 在 AppDelegate 中存储、访问和编辑字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11915538/