2012-08-11 19 views
0

嗨访问和编辑字符串我已经看到了这个问题的答案:存储,在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 

我现在需要访问它并在一个视图中进行更改。然后在另一个视图中显示它。上一页的答案简要解释了这一点,但我在答案的第二部分遇到了问题。它不会让我这样做:

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

任何想法请吗?

谢谢

回答

4

问题是双重的。首先,你正试图访问一个类的ivar,其次,它是一个类而不是一个实例。 [[UIApplication sharedApplication] delegate];返回委托类的有效实例作为单身人员在多个位置轻松访问,但需要将伊娃作为@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; 
+0

谢谢,我一直在学习感谢这个网站。 @synth允许accesors和mutators是啊?我会在两个视图中合成它,还是在AppDelegate.m中使用它? – dev6546 2012-08-11 14:43:53

+0

您只能在.m文件中同步.m文件的名称与您在其中声明的.h文件的名称相同。也就是说,您只能在AppDelegate.m文件中合成窗口和commString。 – CodaFi 2012-08-11 14:46:29