macOS: Problem moving child window with Parent window

I have written a sample code in objective-c to draw two windows and making the first window the parent of 2nd window, so that when first window is moved, second window also moves. I can see two windows are drawn, but the child window is not moving when I move the parent window. What is wrong with this code?

NSRect frame = NSMakeRect(0, 0, 200, 200);
NSUInteger styleMask = NSTitledWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];

NSWindow * window =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered    defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront:NSApp];

NSRect frame1 = NSMakeRect(0, 0, 100, 100);
NSUInteger styleMask1 =  NSTitledWindowMask;
NSRect rect1 = [NSWindow contentRectForFrameRect:frame1 styleMask:styleMask1];

NSWindow * window1 =  [[NSWindow alloc] initWithContentRect:rect1 styleMask:styleMask1 backing: NSBackingStoreBuffered    defer:false];
[window1 setBackgroundColor:[NSColor greenColor]];
[window1 makeKeyAndOrderFront:NSApp];

[window1 setParentWindow:window];


  • Issue 1:

    setParentWindow isn't executed. CFRunLoopRun:

    Runs the current thread’s CFRunLoop object in its default mode indefinitely.

    Set the parent window before CFRunLoopRun.

    Issue 2:

    From the documentation of parentWindow:

    This property should be set from a subclass when it is overridden by a subclass’s implementation. It should not be set otherwise.

    Use addChildWindow:ordered: instead.

    [window addChildWindow:window1 ordered:NSWindowAbove];