objective-c - 在 AppDelegate 中存储、访问和编辑字符串

标签 objective-c ios nsstring appdelegate

嗨,我看到了这个问题的答案:

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/

相关文章:

objective-c - 在两个 NSString 中保留换行符

ios - 计算字符串中数组中的对象数量

ios - 在 Swift 和 Objective C 上使用构建配置

ios - UICollectionView 单元格改变颜色

html - 如何从 iOS 中的文件中解析 JSON?

ios - 在 SearchTextField.swift 中遇到错误

ios - 如何增加 UILabel 中文本及其下划线之间的空间?

ios - 在 Swift 中的数组中添加引号 (")

ios - CanOpenUrl 方法不适用于 ios 9

ios - 在 IOS 中将图像发布到 PHP Web 服务