如何将操作发送到文件所有者以外的目标?
我正在尝试将按钮触摸操作发送到控制器而不是充当文件所有者的控制器。我有四个不同的屏幕区域,我希望由四个单独的控制器(按钮控制器、工具栏控制器、文本控制器和图形控制器)管理,第五个控制器(主控制器)控制其他四个控制器。 mainController 也是与文件所有者关联的控制器。要让 mainController 处理按钮按下操作很简单:
MainController.h 文件:
- (IBAction)buttonIsTouched:(id)sender;
MainController.m 文件:
- (IBAction)buttonIsTouched:(id)sender {
..handle button touch event etc.
}
然后在 Interface Builder 中,将按钮 Touch Down 事件与文件的所有者关联起来,然后选择 buttonIsTouched
就可以了。一切正常。 但是,当我对不是文件所有者控制器的控制器执行完全相同的操作时,应用程序崩溃。这就是我所做的:
创建四个控制器作为 mainController 的实例变量。 在 -[MainController viewDidLoad]
中实例化它们。 提供与上面完全相同的按钮处理方法 在 Interface Builder 中,将对象模板从 Library 拖拽(四次)到 mainController.xib 浏览器上。 将这些对象的类型分别设置为 ButtonController
、ToolBarController
、TextController
和 GraphicsController
。 将 Touch Down 按钮事件与 ButtonController
对象关联,并选择 buttonIsTouched
条目(在小弹出框中) 构建并运行应用程序 单击按钮 崩溃 - 抱歉,我没有写下错误代码,但它类似于 INVALID_ACCESS
当然,我可以让所有输入首先进入 mainController,然后委托给每个单独的控制器,但这似乎是一个不优雅的解决方案,特别是因为我想要下层控制器在向上游向主控制器发送消息之前执行一些工作。如果让我烦恼的话,必须通过 mainController 然后返回。
如果有人知道如何做到这一点并且有一些示例代码可以完成与我想做的类似的事情,我将不胜感激。
交流电
I am trying to send actions from button touches to a controller other than the one acting as File's Owner. I have four distinct areas of the screen that I would like managed by four separate controllers (buttonController, toolbarController, textController and graphicController) with a fifth controller (mainController) controlling the other four. mainController is also the one associated with File's Owner. To get button presses to be handled by mainController is easy:
MainController.h file:
- (IBAction)buttonIsTouched:(id)sender;
MainController.m file:
- (IBAction)buttonIsTouched:(id)sender {
..handle button touch event etc.
}
Then in Interface Builder, associate the button Touch Down event with File's Owner and select buttonIsTouched
and away you go. All works fine.
However, when I do exactly the same thing for a controller that is not the File's Owner controller the application crashes. Here is what I did:
Create four controllers as instance variables of mainController.
Instantiate them in -[MainController viewDidLoad]
.
Provide a button handling method exactly as above
In Interface Builder, drag an Object template from Library (four times) onto the mainController.xib browser.
Set the type for these objects to ButtonController
, ToolBarController
, TextController
and GraphicsController
respectively.
Associate the Touch Down button event with the ButtonController
object and select the buttonIsTouched
entry (in the little pop-up box)
Build and run the application
Click the button
Crash - sorry, I didn't write down the error code but it was akin to INVALID_ACCESS
Of course, I can have all the input go first to mainController and then delegate down to each individual controller but this seems an inelegant solution especially since I want the lower controllers to do some work before messaging upstream to mainController. Having to go through mainController and then back kind if irks me.
If anyone knows how to do this and has some sample code that does something similar to what I want to do I would appreciate it.
ac
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的方法的一个问题是,您为四个子控制器
ButtonController
、ToolBarController
、TextController
和GraphicsController< 中的每一个实例化了两个对象。 /代码>。您正在
viewDidLoad
中以编程方式创建控制器,但它们已从加载的笔尖实例化。您不应在
viewDidLoad
中创建控制器,而应使用MainController
中保留的IBOutlet
属性将它们连接到 IB 中。然后控制器对象由 MainController 拥有,并且在 nib 加载后不会被删除。这也将消除您的记忆错误。
One problem of your approach is that your instantiate two objects for each of your four sub controllers
ButtonController
,ToolBarController
,TextController
andGraphicsController
. You’re creating the controllers programmatically inviewDidLoad
, but they have already been instantiated from the loaded nib.You shouldn’t create the controllers in
viewDidLoad
, but instead use retainingIBOutlet
properties in yourMainController
to wire them up in IB.Then the controller objects are owned by the
MainController
and are not removed after nib loading. This will also remove your memory error.