为什么我不能将我的菜单连接到我的视图控制器IBAction? [英] Why can't I connect my menu to my view controller IBAction?

查看:199
本文介绍了为什么我不能将我的菜单连接到我的视图控制器IBAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于文档的应用程序。我刚刚在故事板和IBActions在我的视图控制器中创建菜单项。但是通常的方式我连接一个动作到目标不工作

   - (IBAction)markAsHidden: 
- (IBAction)markAsVisible:(id)sender;
- (IBAction)toggleHidden:(id)sender;

这是我从我的菜单项中看到的,我按Ctrl和鼠标单击从菜单到View Controller 。它不显示我的IBActions。



任何想法?我的2美分猜测是,它与应用程序是基于文档,但不是真的确定



解决方案


我将菜单连接到我的视图控制器IBAction?


因为您的菜单项和视图控制器位于不同的 / em>。您可以将场景想象为从场景从故事板加载时实例化的对象的独立图。不同场景中的对象不能在故事板中连接在一起,因为它们不会同时加载。



为了有趣,请尝试创建视图的实例您的故事板中的应用程序场景中的控制器。要做到这一点,你可能需要将一个简单的 NSObject 实例拖到场景中,然后设置它的类型。一旦你这样做,你会发现你可以将一个连接从一个菜单项拖动到该视图控制器,就像你所期望的,但是你不能拖动连接到一个不同的对象类型在不同的

注意:
一旦你玩够了足以说服你自己的工作,记住要删除你添加的视图控制器。没有视图的视图控制器就像一个没有qu a的鸭子,视图控制器及其视图层次结构应该在自己的场景中。


我的2美分猜测是它与应用程序是基于文档


不,它没有任何事情接着就,随即。在应用程序中没有基于文档的问题。如果你的应用程序是基于.xib的,而不是使用storyboards,你也会有同样的问题,因为你试图连接到的控制器将在一个完全不同的.xib文件。



Mark已经描述的简单解决方案是使用响应器链。 <第一响应者代理对象是每个场景的一部分,因此您可以随时建立连接。当您将菜单项连接到第一响应程序时,其目标将是 nil ,它告诉 NSMenu 它找到响应菜单项的动作消息的对象。然后将消息发送到该对象。


I have a document based application. I have just created menu items in the storyboard and IBActions in my view controller. However the usual way I connect an action to a target doesn't work

-(IBAction) markAsHidden:(id)sender;
-(IBAction) markAsVisible:(id)sender;
-(IBAction) toggleHidden:(id)sender;

Here is what I see when from my menu item I press Ctrl and mouse click from menu to View Controller. It does not show my IBActions.

Any idea ? My 2 cents guess is that it has to do with the app being document based but... not really sure

解决方案

Why can't I connect my menu to my view controller IBAction?

Because your menu items and view controller are in different scenes in the storyboard. You can think of a scene as an independent graph of objects that are instantiated when the scene is loaded from the storyboard. Objects in different scenes can't be connected together in the storyboard because they're not loaded at the same time.

Just for fun, try creating an instance of your view controller in the Application Scene in your storyboard. To do that, you'll probably need to drag a plain old NSObject instance into the scene and then set its type. Once you do that, you'll find that you can drag a connection from a menu item to that view controller just as you'd expect, but you can't drag a connection to a different object of the very same type in a different scene.

Note: Once you've played around enough to convince yourself that it works, remember to delete the view controller that you added. A view controller without a view is like a duck without a quack, and a view controller and its view hierarchy should be in their own scene.

My 2 cents guess is that it has to do with the app being document based

No, it doesn't have anything to do with that. You'd have the same problem in an app that's not document-based. You'd also have the same problem if your app were .xib-based instead of using storyboards, since the controller you'd be trying to connect to would be in a completely different .xib file.

The easy solution, as Mark already described, is to use the responder chain. The First Responder proxy object is part of every scene, so you can always make connections to it. When you connect a menu item to First Responder its target will be nil, which tells NSMenu to walk the responder chain until it finds an object that responds to the menu item's action message. It then sends the message to that object.

这篇关于为什么我不能将我的菜单连接到我的视图控制器IBAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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