Appending text to NSTextView

Discussion in 'Mac Programming' started by Aranince, Dec 16, 2008.

  1. Aranince macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #1
    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];
    
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    appendAttributedString: takes an NSAttributedString. It appears you're passing it an NSString.
     
  3. Aranince thread starter macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #3
    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];
    
    }
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    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.
     
  5. Aranince thread starter macrumors 65816

    Joined:
    Apr 18, 2007
    Location:
    California
    #5
    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.
     
  6. johnjay1776 macrumors member

    Joined:
    Jul 25, 2008
    #6
    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".
     

Share This Page