为什么设置initialFirstResponder没有效果? [英] Why does setting initialFirstResponder have no effect?

查看:217
本文介绍了为什么设置initialFirstResponder没有效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的形式(NSWindow),有3个文本字段。 NSWindow的 initialFirstResponder 是'指向'第一个字段( NSTextField )。所有三个文本字段通过 nextKeyView 循环链接。

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.

问题是,来自Xcode的应用程序将专注于应用程序关闭时上次活动(焦点)的文本字段。

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.

例如,如果我命名文本字段,B和C和 initialFirstResponder 设置为A.现在如果我启动应用程序,专注于B,并关闭应用程序,下次启动它时,焦点将是

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...)

编辑

这是在OS X Lion 10.7.1,Xcode 4.1。

This is on OS X Lion 10.7.1, Xcode 4.1.

编辑2:

我找到了一种方法来修复这个...在主窗口(或任何窗口)XIB / NIB文件,点击属性检查器,然后取消选中可恢复框。现在应用程序将不会存储最后一个位置,因此 initialFirstResponder 查看将被遵守并遵循。

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.

推荐答案

欢迎来到Cocoa! :)我怀疑这是发生在新的用户界面保留功能。 (实际上,我刚刚创建了一个带有3个文本字段的简单应用程序,我也看到了这种行为。)因为 Windows会自动还原自己,您将看到很多此行为会自动发生,即使您没有实现它。这可能是可取的 - 大多数应用程序将以这种方式工作,并且用户会期望它。

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.

但是,如果你真的禁用它,你可以通过子类化NSWindow或者NSTextField并覆盖 -encodeRestorableStateWithCoder: 。但是,我绝对建议您单独保留默认行为。

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.

编辑一些进一步的信息:存储在〜/ Library / Saved Application State / com.yourapp.savedState 中。在那里你可以看到一个plist文件的窗口信息。其他文件似乎不容易阅读,但他们可能包含关于哪个字段是第一响应者等的信息。

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.

这篇关于为什么设置initialFirstResponder没有效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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