Why doesn't setting initialFirstResponder affect?

I have a simple form (NSWindow) with 3 text fields. NSWindow initialFirstResponder points to the first field ( NSTextField ). All three text fields are connected to each other using nextKeyView .

The problem is that when I launch the application from Xcode, it will focus on the text field that was the last active (in focus) when the application was closed.

So, for example, if I call the text fields A, B and C and initialFirstResponder set to A. Now, if I run the application, focus on B and close the application, the next time I run it, the focus will be on B.

Why is this and how can I fix it?

(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 in any other window) of the XIB / NIB file, click "Attributes Inspector" and then uncheck the "Restore" box. Now the application will not save the last position, and therefore the observation of initialFirstResponder will be respected and followed accordingly.

+7
source share
1 answer

Welcome to Cocoa! :) I suspect this is happening as part of a new user interface save in OS X Lion. (Actually, I just created a simple application with 3 text fields, and I see this behavior too.) Since windows are automatically restored , you will see a lot of this behavior that happens automatically even if you did not implement it. This is probably desirable - most applications will work this way, and the user will expect this.

However, if you really want to disable it, you can probably do this by subclassing NSWindow or perhaps NSTextField and overriding -encodeRestorableStateWithCoder: But I definitely recommend that you keep the default behavior.


Edit with additional information: the state of the application seems to be stored in ~/Library/Saved Application State/com.yourapp.savedState . There you can see the plist file with window information. Other files do not seem easy to read, but they probably contain information about which field is the first responder, etc.

+7
source

All Articles