Objective-C ++ / Cocoa try to create a window with a button, and not work?

I have to create a C ++ class with C ++ methods that are made from objective-c and use cocoa, but now I run into a problem and just can't figure it out, because I'm pretty new in objective-c. Also the fact is that I should be able to create windows and buttons from C ++. Therefore, when I ever create and run this program, it starts and then instantly turns into a β€œnon-responding” state. Anyway, here is what I got:

window.h

#ifndef WINDOW_H
#define WINDOW_H

class Window {
    public:
        Window();
        void createButton();
};

#endif

Window.mm

#include "Window.h"
#include "Button.h"

Window::Window(){
    NSWindow *window = [[NSWindow alloc]
        initWithContentRect: NSMakeRect(100, 100, 200, 200)
        styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask
        backing: NSBackingStoreBuffered
        defer: NO];
    [window setTitle: @"Test window"];
}

void Window::createButton(){
    Button *button;
    [[this contentView] addSubView: button];
// word this gives warning: no '-addSubView:' method found
// this is probably causing the problem I have
    }

button.h

class Button{
    Button();
};
// There will be more methods here eventually

Button.mm

#include "Button.h"
Button::Button(){
    NSButton *button = [[NSButton alloc]
        initWithFrame: NSMakeRect(14, 100, 120, 40)];
    [button setTitle: @"Hello world"];
    [button setAction: @selector(invisible)];
    [button setBezelStyle: NSRoundedBezelStyle];
    [button setButtonType: NSMomentaryLightButton];
    [button setBezelStyle: NSTexturedSquareBezelStyle];
}

main.cpp

#include "Window.h"
#include "Button.h"

int main(int argc, char *argv[]){
    Window x;
    x.createButton();
}

So, does anyone know why this does not work, as I mentioned, I'm pretty new to Cocoa and Objective-C, still learning: P And yes, I tried to fix it.

+3
2

[[this contentView] addSubView: button];

, . "This" ; . (, NSWindow)

- NSWindow , . , :

#ifndef WINDOW_H
#define WINDOW_H

class Window {
    public:
        Window();
        void createButton();
        NSWindow *window;
};

#endif

:

[[window contentView] addSubview: button]; ( 'addSubview')

. NSButton, .

+3

main , .

.

, Cocoa, ++ API . , Window -, .

+3

All Articles