为什么设置initialFirstResponder没有效果?
我有一个带有 3 个文本字段的简单表单(NSWindow)。 NSWindow 的 initialFirstResponder
“指向”第一个字段 (NSTextField
)。所有三个文本字段都通过 nextKeyView
相互循环链接。
我遇到的问题是,当我从 Xcode 启动应用程序时,它将聚焦于应用程序关闭时最后活动(焦点)的文本字段。
例如,如果我将文本字段命名为 A、B 和 C,并将 initialFirstResponder
设置为 A。现在,如果我启动应用程序,将焦点放在 B 上,然后关闭应用程序,下次启动它时,重点将放在 B 上。
这是为什么?我该如何解决这个问题?
(抱歉,如果这是一个微不足道的问题,这是我在 cocoa 中的第一步...)
编辑:
这是在 OS X Lion 10.7.1、Xcode 4.1 上。
编辑2:
我找到了一种方法来“修复”这个问题...在主窗口(或任何与此相关的窗口)XIB/NIB 文件中,单击“属性检查器”,然后取消选中“可恢复” “ 盒子。现在,应用程序将不会存储最后的位置,因此将尊重并遵循 initialFirstResponder
看到的结果。
I have a simple form (NSWindow) with 3 text fields. NSWindow's initialFirstResponder
is 'pointing' to the first field (NSTextField
). All three text fields are circularly linked to each other via nextKeyView
.
Problem that I have is that when I start the application from Xcode it'll focus on the text field that was last active (in focus) when the application closed.
So for example, if I name text fields A, B and C and initialFirstResponder
is set to A. Now if I start the application, focus on B, and close the application, next time I start it, the focus will be on B.
Why is that and how would I fix this?
(Sorry if this is a trivial question, these are my first steps in cocoa...)
EDIT:
This is on OS X Lion 10.7.1, Xcode 4.1.
EDIT 2:
I found a way to "fix" this... In the main window (or any window for that matter) XIB/NIB file, click on "Attributes Inspector", then uncheck "Restorable" box. Now the application will not store the last position and so the initialFirstResponder
seeing will be respected and followed accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
欢迎来到可可! :) 我怀疑这是作为新的 OS X Lion 中的用户界面保留 功能。 (事实上,我刚刚创建了一个带有 3 个文本字段的简单应用程序,我也看到了这种行为。)因为 Windows 自动恢复自身,即使您没有实现它,您也会看到很多这种行为自动发生。这可能是可取的——大多数应用程序都会以这种方式工作,并且用户会期望它。
但是,如果您确实想要禁用它,您可以通过子类化 NSWindow 或 NSTextField 并覆盖
-encodeRestorableStateWithCoder:
。但是,我绝对建议您保留默认行为。编辑更多信息:应用程序状态似乎存储在
~/Library/Saved Application State/com.yourapp.savedState
中。在那里您可以看到一个包含有关窗口信息的 plist 文件。其他文件似乎不容易阅读,但它们可能包含有关哪个字段是第一响应者等的信息。Welcome to Cocoa! :) I suspect this is happening as part of the new user interface preservation features in OS X Lion. (In fact, I just created a simple app with 3 text fields, and I see this behavior too.) Because windows automatically restore themselves, you will see a lot of this behavior happening automatically even if you didn't implement it. This is probably desirable — most applications will work this way, and the user will come to expect it.
However, if you really want to disable it, you can probably do so by subclassing NSWindow or perhaps NSTextField and overriding
-encodeRestorableStateWithCoder:
. But, I definitely recommend you leave the default behavior alone.Edit with a little further information: the app state seems to be stored in
~/Library/Saved Application State/com.yourapp.savedState
. There you can see a plist file with information about the windows. The other files don't seem easily readable, but they probably contain information about which field is first responder, etc.尽管这个帖子已经有近 10 年历史了,但我还是要添加一个答案。就在 jbandes OS X 10.7 Lion 的答案推出后大约一个月。
引用
NSWindowRestoration.h
Despite this thread being almost 10 years old I'll gonna add an answer. Just about one month after the answer from jbandes OS X 10.7 Lion was introduced.
Following a quote from
NSWindowRestoration.h