Question for anyone that knows Cocoa:
Okay, I know C, and just learned Obj-C and some Cocoa. So I'm writing a text-adventure program (gotta start small!) in Cocoa using Xcode and Interface Builder. Everything's going well at the moment, except that the method that actually prints output text apparently only works when it's called from another method of the same class. Basically I made several subclasses of NSObject (I'd rather have multiple classes and code files, just for organizational purposes). Anyway, this method, pOutput:, which takes an NSString as its argument and prints it to an NSTextView in my app, works fine when it's called from another method of its class, but doesn't seem to work when I call it from another class (well, object).
Here's the method (I found the code online, somewhere or other):
Using that NSLog, I've assured that it always gets its argument (outputString) properly, no matter where it's called from, and that the calling method is always able to find pOutput. The only problem is actually displaying the text itself. textView is an instance variable (a pointer to my NSTextView) of the same class that contains pOutput. Xcode never gives me any warnings with this or anything, and like I said, it works fine when it's called from the same class (and therefore same source file), but doesn't work when it's called from some other class.
Thanks to anyone who can help me with this. Obviously a temporary solution would just be to give each class its own pOutput method to call for itself, as in [self pOutput
"print this text"];, but I'd like to know what's wrong here.
edit: I'm not fully correct in this post; see next post.
Okay, I know C, and just learned Obj-C and some Cocoa. So I'm writing a text-adventure program (gotta start small!) in Cocoa using Xcode and Interface Builder. Everything's going well at the moment, except that the method that actually prints output text apparently only works when it's called from another method of the same class. Basically I made several subclasses of NSObject (I'd rather have multiple classes and code files, just for organizational purposes). Anyway, this method, pOutput:, which takes an NSString as its argument and prints it to an NSTextView in my app, works fine when it's called from another method of its class, but doesn't seem to work when I call it from another class (well, object).
Here's the method (I found the code online, somewhere or other):
Code:
- (void)pOutput:(NSString *)outputString
{
NSLog(@"pOutput:; outputString = %@", outputString);
NSRange selectionRange = NSMakeRange([[textView textStorage] length], 0);
[textView setEditable:YES];
[textView setSelectedRange:selectionRange];
[textView insertText:outputString];
[textView setEditable:NO];
[textView display];
}
Using that NSLog, I've assured that it always gets its argument (outputString) properly, no matter where it's called from, and that the calling method is always able to find pOutput. The only problem is actually displaying the text itself. textView is an instance variable (a pointer to my NSTextView) of the same class that contains pOutput. Xcode never gives me any warnings with this or anything, and like I said, it works fine when it's called from the same class (and therefore same source file), but doesn't work when it's called from some other class.
Thanks to anyone who can help me with this. Obviously a temporary solution would just be to give each class its own pOutput method to call for itself, as in [self pOutput
edit: I'm not fully correct in this post; see next post.