0

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

如何将值从一个视图传递到另一个视图

我遇到了一些麻烦。我在 AppDelegate 的头文件中存储了一个字符串。

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    NSString *commString;
}

@property (strong, nonatomic) UIWindow *window;

@end

我现在需要访问它并在一个视图中更改它。然后在另一个视图中显示它。上一页的答案简要说明了这一点,但我对答案的第二部分有疑问。它不会让我这样做:

AppDelegate.commString = myString; //mystring being an NSString

请问有什么想法吗?

谢谢

4

1 回答 1

4

问题是双重的。首先,您正在尝试访问类上的 ivar,其次,它是一个类而不是实例。 [[UIApplication sharedApplication] 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;
于 2012-08-11T14:34:14.877 回答