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];
}
button.h
class Button{
Button();
};
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.
user1632861