rytis
rytis

Reputation: 2721

Why does setting initialFirstResponder have no effect?

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.

Upvotes: 7

Views: 2251

Answers (2)

Julian F. Weinert
Julian F. Weinert

Reputation: 7560

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

@interface NSWindow (NSUserInterfaceRestoration)

/* Determines whether the window should be restored on relaunch.  By default, windows with NSTitledWindowMask set in the styleMask are restorable, and windows without it set are not.
 */
@property (getter=isRestorable) BOOL restorable API_AVAILABLE(macos(10.7));

Upvotes: 0

jtbandes
jtbandes

Reputation: 118671

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.

Upvotes: 7

Related Questions