Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Aranince

macrumors 65816
Original poster
Apr 18, 2007
1,104
0
California
I'm trying to learn ObjC/Cocoa...but it's working against me.

I created this class:
Code:
#import <Cocoa/Cocoa.h>

@interface TestObject : NSObject {
    IBOutlet id textInput;
    IBOutlet id textOutput;
}
- (IBAction)sender:(id)sender;
@end

The textInput is connected to a NSTextField, the textOutput is connected to a NSTextView, and sender is connected to a button. Here is the sender code:

Code:
#import "TestObject.h"

@implementation TestObject

- (IBAction)sender:(id)sender {
	NSString *text = [textInput stringValue];
	[textOutput setStringValue: text];
}
@end

I want it to append string to the text view, but it's not working. I also tried this and failed.

Code:
NSTextStorage *storage = [textInput textStorage];
[storage beginEditing];
[storage appendAttributedString:text];
[storage endEditing];
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
appendAttributedString: takes an NSAttributedString. It appears you're passing it an NSString.
 

Aranince

macrumors 65816
Original poster
Apr 18, 2007
1,104
0
California
Thanks for the reply, but it doesn't work:

Code:
- (IBAction)sender:(id)sender {
	NSString *text = [textInput stringValue];
	
	NSAttributedString *string = [[NSAttributedString alloc] initWithString:text];
	NSTextStorage *storage = [textOutput textStorage];
	
	[storage beginEditing];
	[storage appendAttributedString:string];
	[storage endEditing];

}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Works for me. Are you sure your textOutput and textInput are connected properly in IB? I'd suggest changing their definitions to:
Code:
IBOutlet NSTextField *textInput;
IBOutlet NSTextView *textOutput;
This will force IB to only allow connecting those outlets to objects of their types, whereas with "id" IB doesn't know what kind of objects they are, so it'll let you connect them to anything. You most likely have textOutput connected to the text view's scroll view instead of the text view itself. You can also check this by putting a NSLog(@"%@", textOutput); statement in your sender: method.
 

Aranince

macrumors 65816
Original poster
Apr 18, 2007
1,104
0
California
Ok, thanks I got it working. What I did was right click on the object in IB and connected it to the textview that way instead of connecting it from the TextView to the Object. It wasn't connecting for some reason.
 

johnjay1776

macrumors member
Jul 25, 2008
81
0
Don't feel too bad ... It appears we're about at the same place as far as learning the language and I made the same mistake on a similar "project".
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.