目标-C方法调用

问题描述:

我是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

它看起来像什么就是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

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

您是否检查过fileContent已设置在setFilePath被调用的时间?如果你在启动时尝试设置,那么在视图加载之前你可能正在进行调用(操作系统延迟到最后一刻)。

您可以在尝试访问任何界面构建器视图之前调用[self view]来强制加载视图(请注意,不要调用loadView - 这不符合您的想法)。

+0

谢谢。我用“viewDidLoad”“解决了”我的问题,这很好地工作。但是,你可以举例说明如何使用[自我观察]? – 2010-11-10 08:44:08

如果问题是setFilePath:不叫我猜想,你的代码看起来像

filePath = @"some value"; 

当它应该是

self.filePath = @"some value"; 

当使用@property你需要使用自.filePath来调用方法,否则你将直接访问伊娃。

+0

setFilePath被调用,这不是问题。但是,如果我尝试在同一个类中调用setFilePath中的其他方法,那么被调用的方法不会被“调用”(代码不会执行)。不管怎么说,还是要谢谢你! – 2010-11-09 13:01:42