changing NSTextView string value

Discussion in 'Mac Programming' started by abcdefg12345, Feb 2, 2014.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    I'm trying to use buttons to change the text on text view but i keep getting errors with this method

    AppDelegate.h
    PHP:
    #import <Cocoa/Cocoa.h>

    @interface AppDelegate NSObject <NSApplicationDelegate>
    @
    property (assignIBOutlet NSWindow *window;

    @
    property (weakIBOutlet NSButton *button1;
    @
    property (weakIBOutlet NSButton *button2;
    @
    property (weakIBOutlet NSButton *button3;
    @
    property (weakIBOutlet NSTextView*textfield;
    AppDelegate.m
    PHP:
    #import "AppDelegate.h"

    @implementation AppDelegate

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        
    // Insert code here to initialize your application
    }


    - (
    IBAction)button1:(id)sender{
       
    _textfield.stringValue = @"exampletext1";
    }

    - (
    IBAction)button2:(id)sender{
        
    _textfield.stringValue = @"exampletext2";
    }

    - (
    IBAction)button3:(id)sender{
        
    _textfield.stringValue = @"exampletext3";
    }
    but i keep getting errors:
    Code:
    @property (weak) IBOutlet NSTextView*textfield;
    synthesizing__weak instance variable of type NSTextview*', which does not support weak references

    Code:
    - (IBAction)button1:(id)sender{
       _textfield.stringValue = @"exampletext3";
    }
    
    - (IBAction)button2:(id)sender{
        _textfield.stringValue = @"exampletext2";
    }
    
    - (IBAction)button3:(id)sender{
        _textfield.stringValue = @"exampletext3";
    }
    property 'stringValue' not found on object of NSTextview

    and if i change
    Code:
    @property (weak) IBOutlet NSTextView*textfield;
    
    to

    Code:
    @property (weak) IBOutlet NSTextField *textfield;
    everything works but i can't link it with text view it only links with text fields and labels
     
  2. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #2
    i found another method that is working but i keep on getting warning unused variable 'mystring'

    testAppDelegate.h
    Code:
    #import <Cocoa/Cocoa.h>
    
    @interface testAppDelegate : NSObject <NSApplicationDelegate> {
        
        IBOutlet NSTextView *textView;
    }
    @property (assign) IBOutlet NSWindow *window;
    
    - (IBAction)button1:(id)sender;
    - (IBAction)button2:(id)sender;
    @end
    testAppDelegate.m
    Code:
    #import "testAppDelegate.h"
    
    @implementation testAppDelegate
    
    @synthesize window;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        // Insert code here to initialize your application
    }
    
    -(void)awakeFromNib
    {
        NSString *myString = @"this is a string";
        [textView insertText:myString];
    }
    
    
    - (IBAction)button1:(id)sender {
        [textView setString:@"String number 1111111"];
        NSString *myString = [textView string];
    }
    
    - (IBAction)button2:(id)sender {
        [textView setString:@"String number 2222222"];
        NSString *myString = [textView string];
    }
    @end
    warnings are from

    Code:
     NSString *myString = [textView string];
    unused variable 'myString'
     
  3. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Explain the variable scoping. If you understand this you will understand why you have an error.
     
  4. Madd the Sane macrumors 6502a

    Madd the Sane

    Joined:
    Nov 8, 2010
    Location:
    Utah
    #4
    NSTextView can't be weak-referenced in ARC. Instead, use unsafe_unretained.
     
  5. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #5
    .h
    PHP:
    #import <Cocoa/Cocoa.h>

    @interface testAppDelegate NSObject <NSApplicationDelegate> {
        
        
    IBOutlet NSTextView *textView;
    }
    @
    property (assignIBOutlet NSWindow *window;

    - (
    IBAction)button1:(id)sender;
    - (
    IBAction)button2:(id)sender;
    @
    end
    .m
    PHP:
    #import "testAppDelegate.h"

    @implementation testAppDelegate

    @synthesize window;

    - (
    void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        
    // Insert code here to initialize your application
    }

    -(
    void)awakeFromNib
    {
        
    NSString *myString = @"this is a string";
    }


    - (
    IBAction)button1:(id)sender {
        [
    textView setString:@"whatever"];
    }

    - (
    IBAction)button2:(id)sender {
        [
    textView setString:@"abcdefg"];
    }
    @
    end
    just had to get rid of

    PHP:
    [textView insertText:myString];
    and problem solved
     

Share This Page