Seneca
Seneca

Reputation: 2412

cocoa add text to window programmatically

I'm trying to get my feet wet with some very basic Cocoa programming in Objective-c without using Xcode. This is mainly for learning purposes and not for real world app development.

I created a "hello world" program with the following source code:

#import <Cocoa/Cocoa.h>

int main(int argc, const char * argv[]) {
    NSApplication* app = [NSApplication sharedApplication];
    id window =
         [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 400)
                                            styleMask:NSWindowStyleMaskTitled
                                                 backing:NSBackingStoreBuffered
                                                   defer:NO];
    [window setTitle:@"Hello world"];
    [window makeKeyAndOrderFront:nil];
    [window center];


    NSText* t = [[NSText alloc] initWithFrame:NSMakeRect(20,20,100,100)];
    [t setString:@"test"];

    [window addSubview:t];

    [app run];

    return 0;
}

In the example above I'm trying to add some simple text "Test" to the window. However as soon as I start my compiled app, I get:

-[NSWindow addSubview:]: unrecognized selector sent to instance 0x7fb8e0611270
2017-06-24 14:34:02.777 lala[39810:1252869] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSWindow addSubview:]: unrecognized selector sent to instance 0x7fb8e0611270'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff93bcf2cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffa89da48d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff93c50f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff93b41755 ___forwarding___ + 1061
    4   CoreFoundation                      0x00007fff93b412a8 _CF_forwarding_prep_0 + 120
    5   lala                                0x0000000103b0feab main + 651
    6   libdyld.dylib                       0x00007fffa92bf235 start + 1
    7   ???                                 0x0000000000000001 0x0 + 1
)

Why does my app crash with [NSWindow addSubview:]: unrecognised selector sent to instance? How do I add text to a window programmatically?

Upvotes: 1

Views: 949

Answers (2)

Symanski
Symanski

Reputation: 337

Came here looking for a solution to adding the NSTextfield to the view, but it confirmed what I had been trying to use already. Instead I tried using "setContentView" but then the text filled the view, whereas I need it to work as a subview. So here's my code:

NSTextField *yourLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, width , height * 1.0/3.0)];
    yourLabel.editable = false;
    yourLabel.bezeled = true;
    [yourLabel setTextColor:[NSColor blackColor]];
    [yourLabel setBackgroundColor:[NSColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.5]];
    [yourLabel setFont:[NSFont fontWithName:@"GurmukhiMN-Bold" size:(height/24)]];
    yourLabel.stringValue = [NSString stringWithFormat:@"The Second Output Is Operational"];
    yourLabel.alignment = NSTextAlignmentCenter;
    [self.window.contentView addSubview:yourLabel];

The key difference is where I'm sending the message to. Seems that is the best way to add a subview and others cause race conditions or crashes.

For completeness I got the width and height earlier (they're used throughout the view so grab them once):

height = self.window.frame.size.height;
width = self.window.frame.size.width;

Upvotes: 0

Artem E.
Artem E.

Reputation: 23

Try to use NSTextField instead of NSText:

NSRect frameRect = NSMakeRect(20,20,100,100)
NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
[myView addSubView:myTextField];

Moreover you need to make either AppDelegate-based app or Storyboard-based app, so that you could use NSView to display your objects.

Upvotes: 1

Related Questions