Re: Unable to create outlets/connections for NSMenuItems, but other controls work
Re: Unable to create outlets/connections for NSMenuItems, but other controls work
- Subject: Re: Unable to create outlets/connections for NSMenuItems, but other controls work
- From: Alex Hall <email@hidden>
- Date: Wed, 02 Sep 2015 23:02:05 -0400
> On Sep 2, 2015, at 5:46 PM, Ken Thomases <email@hidden> wrote:
>
> On Sep 2, 2015, at 4:19 PM, Alex Hall <email@hidden> wrote:
>
>> Regarding connection to First Responder, I think I'm still missing a step. I have a menu item I want to connect to First Responder so that, as I understand it, I can then make an outlet and action for that item in my view controller (stop me if I'm wrong there).
>
> You won't be able to have an outlet of the view controller connect to the menu item. Connecting the menu item's action to First Responder doesn't change that. It just allows the menu item to invoke the action on the view controller through the indirect mechanism of the first responder and the responder chain originating there. (But only if the view controller is in that responder chain due to its view or a subview having focus.)
Yes, right after I sent this message, I realized that an outlet was impossible, and not the point. I was in a bit of a rush earlier and wanted to get this off before I had to leave.
>
>> Here's what I did:
>>
>> * select the menu item in the storyboard's outline table
>> * go to the Connections Inspector and find the "Sent Actions"
>> * There's a button in there that VO identifies as "connection action, AX button". I locked the mouse down on that.
>> * I went back to my outline, located First Responder in my application scene, put VO on the unlabeled button on to its right, and released the mouse. A connection appeared in the Connections Inspector for First Responder, so that seems to have worked.
>> * Note: a new window appeared, with a massive list of actions, from "superscript" to "cut" to "spellcheck", and tons more. VO read a few unlabeled buttons in here, but mostly it just found objects that were "unknown". Enter did nothing, so I eventually had to press escape. I still don't know what that window was for or if I was supposed to do something with it.
>
> Connecting the menu item's action actually needs to set two pieces. First is the target. You've successfully chosen the First Responder placeholder. (As previously discussed, this actually results in the menu item having nil for its target which is what you want.) The second piece is which action method it should invoke. That second window is a menu to allow you to select the action method. First Responder lists all known action methods of all known responder classes because any responder could be in the responder chain. That's why the list is so long. The menu supports type-to-select, although it doesn't quite work like I'm used to.
>
> If you didn't successfully select something from that menu, then either the action connection wasn't made or it was made to some arbitrary action method.
Okay, I just tried this process again, and this time it worked! Thanks to your hint about type-to-select, I was able to type in a few letters of the function I wanted and find it. VO identifies every possible function in this menu as "unknown", but forcing a click on it (vo-cmd-f5, vo-shift-space) seems to do the job just the same.
>
>> * If I'm following yesterday's discussion, I should now be able to make an IBOutlet in my view controller for the menu item, since said menu item is now hooked to First Responder.
>
> Nope.
Nope. :)
>
>> When I did, it refused to connect.
>
> Right.
>
>> * In case I misunderstood, and I have to keep the outlet in AppDelegate but can put just the action anywhere, I connected the menu item to an outlet in AppDelegate and then tried dragging from the "new action AX button" in the menu item's Connections Inspector to my IBAction method, in the view controller. Unfortunately, the mouse simply refused to route to the button I needed to drag, yet it routed to any other connection button I tried.
>
> First, do you actually need an outlet to the menu item? What do you plan to do with that? It's not required to get the menu item working. It might be useful if you wanted to do run-time manipulation of the menu item, but there are also other (likely better) ways of doing that, like menu validation. That would keep the manipulation inside the class that implements the action method.
I don't need it, you're right about that. I did plan to change the menu item's title dynamically, but as you say, there are better ways. I'm just used to "connect the outlet, connect the action" and was following the ant trail, as it were.
>
> If you really do need an outlet, it has to be from the app delegate. It sounds like you were able to make that connection. Connecting the menu item's action is unrelated. You described earlier doing that successfully, too, although there's some doubt about whether you were able to select the correct action method.
>
> Something to consider if Xcode's interface is just not accessible enough (after filing a bug!) would be to connect an outlet from the app delegate and then have the app delegate set the item's target and action in code, such as in the -applicationDidFinishLaunching: method. The code would reference the menu item via the outlet. So, it would be something like:
>
> self.myMenuItem.target = nil;
> self.myMenuItem.action = @selector(myActionMethod:);
Oddly, this resulted in a fatal error when unwrapping an optional, which appears to be on the line setting the target or the action. It's a moot point since I seem to be able to use that menu that appears when connecting to First Responder, but it's curious nonetheless. I should've mentioned this is Swift, but that probably won't have any bearing. Still, the GUI method of hooking this up seems to work--I just did it for a second menu item, and though it took three attempts, the menu of all the actions finally appeared and I was able to select one. That method then executed when I ran the app and chose the menu item! Thanks again for all the help, I really appreciate it.
>
> Regards,
> Ken
>
--
Have a great day,
Alex Hall
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden