2012-08-11 35 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