2010-11-09 79 views
1

我是Objective-C和iPhone SDK開發的新手。我想調用一個方法,在同一個班級:目標-C方法調用

- (void) setFilePath:(NSString *) p 
{ 
    [self methodCall]; 
} 

- (void) methodCall 
{ 
    fileContent.text = @"Test"; //fileContent is a UITextView 
} 

如果屬性「文件路徑」設置,方法「setFilePath」之稱。然後在IB中創建的UITextView應顯示文本。但是,這並不工作...

如果我在IB直接通過按鈕來調用方法,那麼UITextView的成功改變了他的內容:

- (IBAction) clickButton 
{ 
    fileContent.text = @"Test"; 
} 

可能是什麼問題呢?

感謝您的回答!

編輯2:我解決了推視圖後設置 「文件路徑」 的問題:

- (IBAction) showFileContent { 

FileContentsViewController *fileContentsViewController = [[FileContentsViewController alloc] init]; 
[self.navigationController pushViewController:fileContentsViewController animated:YES]; 
fileContentsViewController.filePath = self.filePath; 
fileContentsViewController.title = [NSString stringWithFormat:@"Content from von %@", [filePath lastPathComponent]]; 
[fileContentsViewController release]; 

} 

編輯1:這是我的界面代碼:

@interface FileContentsViewController : UIViewController { 

NSString *filePath; 
UITextView *fileContent; 

} 

- (void) methodCall; 

@property (nonatomic, retain) NSString *filePath; 
@property (nonatomic, retain) IBOutlet UITextView *fileContent; 

@end 

。 ..這是實現的代碼:

#import "FileContentsViewController.h" 


@implementation FileContentsViewController 

@synthesize filePath; 
@synthesize fileContent; 

- (void) setFilePath:(NSString *) p 
{ 
    NSLog(@"setFilePath executed!"); 
    [self methodCall]; 
} 

- (void) methodCall 
{ 
    fileContent.text = @"Test"; // UITextView 
} 

// some standard methods 

@end 

...最後,設置「文件路徑」的方法的代碼:

- (IBAction) showFileContent { 

FileContentsViewController *fileContentsViewController = [[FileContentsViewController alloc] init]; 
fileContentsViewController.filePath = self.filePath; 
fileContentsViewController.title = [NSString stringWithFormat:@"Content from von %@", [filePath lastPathComponent]]; 
[self.navigationController pushViewController:fileContentsViewController animated:YES]; 
[fileContentsViewController release]; 

} 
+1

顯示如何調用setFilePath: – JeremyP 2010-11-09 09:05:58

+0

[XCode Debugger](http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/XcodeDebugging/000-Introduction/Introduction.html%23/)/apple_ref/doc/uid/TP40007057-CH1-SW1)也應該能夠告訴你代碼在做什麼。 – outis 2010-11-09 09:32:14

+0

@JeremyP我在設置屬性「filePath」時調用它。該屬性如下所示:@property(nonatomic,retain)IBOutlet UITextView * fileContent。 – 2010-11-09 09:48:15

回答

1

它看起來像什麼就是fileContentsViewController-showFileContent創建沒有任何分配給它的FileContentsViewController.fileContent(或者,至少,fileContent不指向獲取顯示一個UITextView)時fileContentsViewController.filePath設置。

您在創建fileContentsViewController後立即設置filePath。如果FileContentsViewController-init未創建適當的fileContent,則當從-showFileContent調用-setFilePath:時,沒有fileContent來設置text。如果fileContentsViewController是典型的視圖控制器,fileContent將不存在,直到fileContentsViewController被加載,這(我相信)發生在-pushViewController:animated期間。

一個解決方法是重寫-setFileContent設置fileContent.text酌情:

-(void)setFileContent:(UITextView*)fileContentView { 
    if (fileContent != fileContentView) { 
     [fileContent release]; 
     fileContent = [fileContentView retain]; 
     if (self.filePath) { // if file path is not nil 
      fileContent.text = ...; 
     } 
    } 
} 

另外其他的解決方法是,以確保您只設置filePathfileContent存在,但這是更脆。三分之一是在按fileContentsViewController後設置filePath

在調試過程中發現原因的方法是檢查兩件事:execution(「我想要執行的代碼是否已達到?」)和data(「變量是否保存我期望的值? 「)。在-showFileContent-methodCall中設置斷點,以便知道方法正在被調用(這可能是導致失敗的一個原因)。如果執行到-methodCall,問題一定是別的。從那裏,檢查在-methodCall中使用的變量的值,並且您會發現fileContent不是或者不是相同的fileContent,它稍後顯示。

+0

謝謝你的確切答案!在我推入fileContentsViewController後,我通過設置filePath解決了這個問題。這現在很有用!但是,如果我要用上面的「setFileContent」方法修復它,那麼何時會調用此方法?當視圖加載並且UITextView被初始化時?也感謝您在調試中的提示。我發現「methodCall」被調用,但也許,正如你所說的,fileContent在這一點上沒有加載。 – 2010-11-13 11:30:22

+0

'setFileContent'是一個標準的setter,因此無論何時設置fileContent屬性(這是該修補程序的美妙之處),包括在加載子視圖時的[fileContentsViewController視圖]期間都會調用它。 – outis 2010-11-14 00:33:09

+0

哎呀,我忘了再次查看此線程。謝謝,outis! – 2010-11-23 21:24:05

0

你如何定義文件路徑屬性?
我認爲這是問題...

+0

你可以在我上面編輯的文章中找到我的定義。 – 2010-11-09 10:05:40

1

您是否檢查過fileContent已設置在setFilePath被調用的時間?如果你在啓動時嘗試設置,那麼在視圖加載之前你可能正在進行調用(操作系統延遲到最後一刻)。

您可以在嘗試訪問任何界面構建器視圖之前調用[self view]來強制加載視圖(請注意,不要調用loadView - 這不符合您的想法)。

+0

謝謝。我用「viewDidLoad」「解決了」我的問題,這很好地工作。但是,你可以舉例說明如何使用[自我觀察]? – 2010-11-10 08:44:08

1

如果問題是setFilePath:不叫我猜想,你的代碼看起來像

filePath = @"some value"; 

當它應該是

self.filePath = @"some value"; 

當使用@property你需要使用自.filePath來調用方法,否則你將直接訪問伊娃。

+0

setFilePath被調用,這不是問題。但是,如果我嘗試在同一個類中調用setFilePath中的其他方法,那麼被調用的方法不會被「調用」(代碼不會執行)。不管怎麼說,還是要謝謝你! – 2010-11-09 13:01:42