2011-10-31 27 views
0

即時新編程,並盯着目標c。我想在一個文件中聲明一個屬性,並使其成爲另一個文件,但由於某種原因,它不起作用。我可能在做非常愚蠢的事情,不要怪我。訪問屬性在目標c中形成一個uiviewcontroler實例。

我有folling第一頭:

#import <Foundation/Foundation.h> 
#import "second.h" 

@interface ViewController : UIViewController{ 
    NSString* theText; 
} 
@property (nonatomic, assign) IBOutlet UITextField *textField; 
@property (nonatomic, retain) NSString *theText; 



@end 

在這裏,我.m文件

#import "ViewController.h" 

@implementation ViewController 
@synthesize theText,textField; 

- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
    [self setTheText:textField.text]; 

} 

@end 

現在我想用theText財產在其他文件中既使用它,並改變它。所以我想這會工作,但它並不:

第二.m文件:

#import "second.h" 
#import "ViewController.h" 

@implementation second 
@synthesize secLabel; 

-(void)nameLabel{ 

    secLabel.text = [ViewController theText]; 
} 

@end 

編譯器說,對選擇沒有已知的類方法。我嘗試了很多,但沒有任何工作,有人知道如何使這項工作?

TNX

回答

0

theText是一個實例級屬性,但您要訪問它的視圖控制器類。相反,您需要在ViewController的某個實例上訪問它。換句話說,你需要:

ViewController *viewController = ... some code to get a ViewController pointer ... 
secLabel.text = [viewController theText]; 

你的「第二」視圖控制器某種程度上需要獲得視圖控制器的一個實例。如何真正得到這取決於你的應用程序,我不能不知道更多關於你的代碼。

0

http://www.galloway.me.uk/tutorials/singleton-classes/

這將告訴你如何正確地做一個單獨的類。

它可能不是最乾淨的方式,但它的存儲數據的正確路徑。

+0

謝謝! 我明白教導,但我不明白在哪裏/如何打電話: MyManager * sharedManager = [MyManager sharedManager]; 你能解釋一下嗎? – user1022722

+0

它在執行任何命令訪問單例類之前必須執行的操作。所以E.G. – Necro

+0

如果你在按下按鈕後改變一個數字,你會像這樣寫MyManager * sharedManager = [MyManager sharedManager]; sharedManager.number + = 1;得到它? – Necro