DYNAMICALLY CREATING COCOA WINDOW:

John Brown

Total Post:6

Points:42
Posted by  John Brown
 862  View(s)
Ratings:
Rate this:

I want to creae Cocoa window at runtime, I was trying to generate dynamic window but it is not working,
this is my code:

NSRect myFrame = NSMakeRect(0, 0, 100, 100);
NSUInteger mask = NSBorderlessWindowMask;
NSRect myRect = [NSWindow contentRectForFrameRect:myFrame mask:mask];

NSWindow * myWindow =  [[NSWindow alloc] initWithContentRect:myRect
                       mask:mask backing: NSBackingStoreRetained
                       defer:false];
[myWindow setBackgroundColor:[NSColor blueColor]];
[myWindow display];

My code is generating error and I am new in iOS so I am unable to solve this problem,
please anyone help me.

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: Dynamically creating Cocoa window:

    The problem is that you don't want to call display, you want to call either makeKeyAndOrderFront or orderFront depending on whether or not you want the window to become the key myWindow. You should also probably use NSBackingStoreBuffered.
    This code will create your borderless, green window at the bottom left of the screen:

    NSRect myFrame = NSMakeRect(0, 0, 100, 100);
    NSWindow* myWindow  = [[[NSWindow alloc] initWithContentRect:myFrame
                        mask:NSBorderlessWindowMask
                        backing:NSBackingStoreBuffered
                        defer:NO] autorelease];
    [myWindow setBackgroundColor:[NSColor greenColor]];
    [myWindow makeKeyAndOrderFront:NSApp];

    - Don't forget to assign myWindow to a strong/retaining property!
    - Under ARC, not doing so will cause it to disappear immediately;
    - without ARC, the window will be leaked.

    You can make the sender for makeKeyAndOrderFront or orderFront whatever is appropriate for your situation.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!