IBAction Running by itself?

I finally made the plunge and switched to Xcode 4. I was testing a simple app to check out popovers, but noticed that my popover was showing up before I did anything. At first I thought I was using the popover wrong, but after adding a log to my action method attached to a text field, I noticed that I was getting the log of “got here” right after the app opened, before doing anything in the text field. Is this some new behavior in Xcode 4? Seems pretty weird.

script RDAppDelegate
	property parent : class "NSObject"
    property po:missing value --IBOutlet for a popover
   	
	on applicationWillFinishLaunching_(aNotification)
	end applicationWillFinishLaunching_
    
    on enteredText_(sender) -- IBAction for a text field
        log "got here"
        po's showRelativeToRect_ofView_preferredEdge_(sender's |bounds|(),sender,2)
    end enteredText_
	
end script

Ric

The version of Xcode shouldn’t make any difference – you can open the same project in Xcode 3 to check.

The first time I ran it in Xcode 3, it worked fine, but the second time it didn’t. I see the same thing in Xcode 4. Also, if I change the text field Action setting to “Send on Enter Only” it works fine. If I then switch back to “Send on End Editing”, it works right the first time, but not thereafter. I’ve never seen anything like this before.

Ric

Feel free toy send a copy if you think that’ll help.

I will. It seems like the problem is that something is changing the first responder – when the window first opens, the text field (that the popover is attached to) is not the first responder, even if I connect the window’s initialFirstResponder property to the text field. If I log the initial first responder, I get a text view – I’m assuming that is the field editor for the text field? When the app starts up, the window doesn’t seem to be key (not selected).

Ric

After Edit: In Xcode 3, the text field does have the focus when the app opens, but the popover is still visible initially. I don’t know if the fact that the window not having focus (when run from Xcode 4) has anything to do with Xcode being on one desktop and the app opening up in another. I’m not sure why it’s doing that (with Xcode 3 I had it set up so Xcode would open in one desktop and IB in another).

It’s very odd. I tried adding another text field, and another action handler, and whichever text field is the initialFirstResponder sends an action at launch. In fact, the popup looks a red herring. I dragged one text field to the top of the window, and it’s handler fired; I swapped them, and the other fired.

All I can think of is a damaged .xib. Are you using Xcode 4.2?

Yep, it’s Xcode 4.2 – just downloaded it today. I’ll try making another project and see if I get the same behavior.

Ric

I see similar behavior in an app that just has 2 text fields connected to 2 actions, each of which just contains one log statement. The first time I run it, it works ok, but in subsequent runs, the action method for the top text field fires on startup. I’ve noticed that which text field has focus on startup depends on which had it when I last closed the program. This is something I’ve noticed before, I think just since upgrading to Lion. It seems as if window size and position as well as what UI element has focus is saved from run to run – I wonder if this could be the source of the problem? Do you know if there’s a way to shut off this behavior so I can test it?

Ric

After Edit: I made the same type of program in Xcode 3 and got exactly the same results.

Ah, of course: click off Restorable for the window.

Ok, that fixed it in Xcode 4. Is that a new feature? I’m seeing the same problem in Xcode 3 but there’s no Restorable to uncheck in IB.

After Edit: Ok, I found that you can do it in code by using setRestorable_(0) in Xcode 3, but there’s no method with that name mentioned in the Docs. Seems to be an undocumented feature in Xcode 3.

I still don’t quite understand why this should have this effect. Even if you leave the focus on the top text field before closing the app, the action method still fires even though that text field starts with the focus when the app reopens. I seems like it somehow loses the focus and then it’s restored, thus firing the method. I found you could get around this in another way by using setAction: in the applicationDidFinishLaunching method (but not in the “will” version) instead of doing it in IB. That way, you can still leave the window restore features intact if you want them.

Ric

It’s new in 10.7, which would explain why it’s not in Xcode 3.

I suspect it’s unintended (i.e., a bug).