macOS:使用父窗口移动子窗口时出现问题 [英] macOS: Problem moving child window with Parent window

查看:172
本文介绍了macOS:使用父窗口移动子窗口时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Objective-C中编写了一个示例代码来绘制两个窗口,并使第一个窗口成为第二个窗口的父窗口,以便在移动第一个窗口时,第二个窗口也移动.我可以看到绘制了两个窗口,但是当我移动父窗口时,子窗口没有移动.此代码有什么问题?

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

推荐答案

问题1:

setParentWindow 未执行. CFRunLoopRun :

无限期地以其默认模式运行当前线程的CFRunLoop对象.

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

CFRunLoopRun 之前设置父窗口.

问题2:

来自 parentWindow 的文档:

当子类的实现重写此属性时,应从子类设置此属性.

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

改为使用 addChildWindow:ordered:.

[window addChildWindow:window1 ordered:NSWindowAbove];

这篇关于macOS:使用父窗口移动子窗口时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆