I’m new to Objective-C and iPhone SDK development. 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. Then the UITextView, created in IB, should display the text. But that doesn’t work …
If I call the method directly via button in IB, then the UITextView changes his content successfully:
- (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:
- (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:
@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”:
- (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];
}
What it looks like is that the
fileContentsViewControllercreated in-showFileContentdoesn’t have anything assigned to itsFileContentsViewController.fileContent(or, at least,fileContentdoesn’t point to a UITextView that gets displayed) whenfileContentsViewController.filePathis set.You set
filePathimmediately after creatingfileContentsViewController. IfFileContentsViewController‘s-initdoesn’t create an appropriatefileContent, then when-setFilePath:is called from-showFileContent, there’s nofileContentto set thetextof. IffileContentsViewControlleris a typical view controller,fileContentwon’t exist untilfileContentsViewControlleris loaded, which (I believe) happens during-pushViewController:animated.One fix is to override
-setFileContentto setfileContent.textas appropriate:Another other fix is to ensure you only set
filePathwhenfileContentexists, but this is more brittle. A third is to setfilePathafter you pushfileContentsViewController.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
-showFileContentand-methodCallso you know that the methods are being called (which would be one reason for failure). If execution makes it into-methodCall, the problem must be something else. From there, examine the values of the variables used in-methodCalland you’ll discoverfileContentis either nil or not the samefileContentthat shows up later.