Set value of NSTextField

I am trying to set a value NSTextField, but it is not working properly.

I have a button associated with IBAction, and when I install it with self, it works fine:

#import <Foundation/Foundation.h>


@interface TestMessage : NSObject {
     IBOutlet NSTextField *text;
}

- (IBAction) setMessage: (id) controller;
- (void) Message:(NSString *) myMessage;    
@end

#import "TestMessage.h"

@implementation TestMessage

- (IBAction) setMessage: (id) controller {
    // This works
    [self Message:@"Hello"];

    // but this doesn't
    TestMessage * messageTest= [TestMessage new];
    [messageTest Message:@"Hi"];

}
- (void) Message: (NSString *) myMessage {
    [text setStringValue: myMessage]; 
    NSLog(@"Message Was Called");
    // This returns <NSTextField: 0x1001355b0> when called 
    // using self, but null when called the other way.
    NSLog(@"%@", text);
}

@end

I searched for a while, but still can't find the answer.

I think this has something to do with the delegate, but I'm not sure.

Thanks in advance.

+3
source share
3 answers

Are you sure the message is called when you call it from anotherFuntion? If it anotherFuntionis a method of another class, the call [self message:]will not work as you expected ...

+1
source

, , . :

[textField stringValue];
0

TestMessage * messageTest = [TestMessage new];

, new. , new - , alloc/init

 TestMessage * messageTest = [[TestMessage alloc] init];

, IBOutlet NSTextField *text , TestMessage Nib. Interface Builder,

Set to load this class in IB

initWithCoder encodeWithCoder - , IB:

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.text = [coder decodeObjectForKey:@"text"];
    }
    return self;
}


-(void)encodeWithCoder:(NSCoder *)coder
{
    [super encodeWithCoder:coder];
    [coder encodeObject:self.text forKey:@"text"];
}

, IBOutlet , . , , A A U, B B UI? Nib.

0

All Articles