Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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];

CFRunLoopRun();
[window1 setParentWindow:window];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
278 views
Welcome To Ask or Share your Answers For Others

1 Answer

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...