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

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
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 (assign) IBOutlet NSWindow *window;

@property (weak) IBOutlet NSButton *button1;
@property (weak) IBOutlet NSButton *button2;
@property (weak) IBOutlet NSButton *button3;
@property (weak) IBOutlet 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
 

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
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'
 

abcdefg12345

macrumors 6502
Original poster
Jul 10, 2013
281
86
.h
PHP:
#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

.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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.