简体   繁体   English

Objective-C中的方法调用

[英]Method call in Objective-C

I'm new to Objective-C and iPhone SDK development. 我是Objective-C和iPhone SDK开发的新手。 I want to call a method in the same class: 我想在同一个类中调用一个方法:

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

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

If the property "filePath" is set, the method "setFilePath" is called. 如果设置了属性“ filePath”,则将调用方法“ setFilePath”。 Then the UITextView, created in IB, should display the text. 然后,在IB中创建的UITextView应该显示文本。 But that doesn't work ... 但这不起作用...

If I call the method directly via button in IB, then the UITextView changes his content successfully: 如果我直接通过IB中的按钮调用该方法,则UITextView成功更改其内容:

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

What could be the problem? 可能是什么问题呢?

Thanks for your answers! 感谢您的回答!

EDIT 2: I solved the problem by setting "filePath" after pushing the view: 编辑2:我通过推送视图后设置“ filePath”解决了问题:

- (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];

}

EDIT 1: Here's the code of my interface: 编辑1:这是我的界面的代码:

@interface FileContentsViewController : UIViewController {

NSString *filePath;
UITextView *fileContent;

}

- (void) methodCall;

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

@end

... and here's the code of the implementation: ...这是实现的代码:

#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

... and finally the code of the method that sets "filePath": ...最后是设置“ filePath”的方法的代码:

- (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];

}

Have you checked that fileContent has been set up at the time setFilePath is called? 您是否在调用setFilePath时检查是否已设置fileContent If you're trying to set things up at start up then it's possible that you're making calls before the views have been loaded (which the OS delays until the last possible moment). 如果尝试在启动时进行设置,则可能是在加载视图之前进行调用(操作系统会延迟到最后一刻)。

You can force views to load by calling [self view] just before you try to access any of your Interface Builder views (NB don't call loadView - that doesn't do what you'd think). 您可以在尝试访问任何Interface Builder视图之前,通过调用[self view]来强制加载视图(注意,不要调用loadView不会执行您想的那样)。

If the problem is that setFilePath: is not called that I would guess that your code looks like 如果问题是没有调用setFilePath:,我猜你的代码看起来像

filePath = @"some value";

when it should be 什么时候应该

self.filePath = @"some value";

When using @property you need to use self.filePath to call the methods, otherwise you will just access the ivar directly. 使用@property时,您需要使用self.filePath来调用方法,否则,您将直接访问ivar。

What it looks like is that the fileContentsViewController created in -showFileContent doesn't have anything assigned to its FileContentsViewController.fileContent (or, at least, fileContent doesn't point to a UITextView that gets displayed) when fileContentsViewController.filePath is set. 它是什么样子的是, fileContentsViewController中创建-showFileContent没有分配给它的任何FileContentsViewController.fileContent (或者,至少, fileContent不指向一个UITextView是被显示)时fileContentsViewController.filePath设置。

You set filePath immediately after creating fileContentsViewController . 您在创建fileContentsViewController之后立即设置filePath If FileContentsViewController 's -init doesn't create an appropriate fileContent , then when -setFilePath: is called from -showFileContent , there's no fileContent to set the text of. 如果FileContentsViewController-init不会创建一个合适的fileContent ,那么当-setFilePath:从所谓-showFileContent ,没有fileContent设置text的。 If fileContentsViewController is a typical view controller, fileContent won't exist until fileContentsViewController is loaded, which (I believe) happens during -pushViewController:animated . 如果fileContentsViewController是典型的视图控制器,则在加载fileContentsViewController之前, fileContent将不存在,(我相信)在-pushViewController:animated期间发生。

One fix is to override -setFileContent to set fileContent.text as appropriate: 一种解决方法是重写-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 = ...;
        }
    }
}

Another other fix is to ensure you only set filePath when fileContent exists, but this is more brittle. 另一个解决方法是确保仅在存在fileContent时设置filePath ,但这比较脆弱。 A third is to set filePath after you push fileContentsViewController . 第三是在推送fileContentsViewController之后设置filePath

The way you would discover the cause during debugging is to check two things: execution ("Is the code I'm expecting to be executed ever reached?") and data ("Do the variables hold the values I expect?"). 在调试过程中发现原因的方法是检查两件事:执行(“是否希望执行我要执行的代码?”)和数据(“变量是否持有我期望的值?”)。 Set breakpoints in -showFileContent and -methodCall so you know that the methods are being called (which would be one reason for failure). -showFileContent-methodCall设置断点,以使您知道方法正在被调用(这将是失败的原因之一)。 If execution makes it into -methodCall , the problem must be something else. 如果执行使它进入-methodCall ,则问题一定是其他问题。 From there, examine the values of the variables used in -methodCall and you'll discover fileContent is either nil or not the same fileContent that shows up later. 从那里,检查-methodCall使用的变量的值,您将发现fileContent为nil或与以后显示的fileContent

How have you define filePath property ? 您如何定义filePath属性?
I think that it is the problem... 我认为这是问题所在...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM