为视图控制器设置插座/动作 [英] Setting outlet/action for view controller

查看:21
本文介绍了为视图控制器设置插座/动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个视图控制器中有一个 UIButton,它不是我的应用程序中的根视图控制器.我无法在我的头文件中拖动并为其创建插座或操作.我只能从根视图控制器执行此操作.

是否有一个简单的原因?

解决方案

当使用 storyboard 时,XCode 只为第一个视图控制器(名为 ViewController.h 和 ViewController.m)创建 .h 和 .m 文件.

>

对于您通过 Interface Builder 添加的每个额外的视图控制器,您应该手动添加额外的自定义类文件以自定义视图控制器.

通过 Interface Builder 将视图控制器添加到故事板后,请按照以下说明操作:

  1. 从 XCode 主菜单中,选择 File、New、File,然后选择Objective-C 类,然后单击下一步.命名自定义类,然后选择您添加的视图控制器的正确类型(即为表视图控制器选择 UITableViewController).
  2. 在 XCode 的 Interface Builder 中,选择新的视图控制器,然后从 Identity Inspector 选项卡中,将类设置为您刚刚创建的新类.

然后,您应该能够按住 CTRL 键并单击将任何 UIButtons 或其他 UI 元素拖动到新的 .h 或 .m 文件上,并为那里的视图实现任何自定义行为.

如果你不创建文件来实现你的自定义类,你就会被像 UIViewController 这样的默认 UI 类卡住,除非你用自定义类扩展它们,否则它们只有默认行为.

通常每个视图控制器需要一个 .m 和 .h 文件.

I have a UIButton in a View Controller that is NOT the root view controller in my app. I cannot drag and create an outlet or an action for it in my header file. I can only do it from the root view controller.

Is there a simple reason for this?

解决方案

When using the storyboard, XCode only creates the .h and .m file for the first view controller (named ViewController.h and ViewController.m).

For each additional view controller that you add via Interface Builder, you should manually add additional custom class files in order to customize the view controller.

Once you add a View Controller to your story board via Interface Builder, follow these instructions:

  1. From the main XCode menu, select File, New, File, then pick Objective-C class and click Next. Name the custom class, and pick the proper type for the view controller that you've added (i.e. select UITableViewController for a Table View Controller).
  2. From within XCode's Interface Builder, select the new view controller and from the Identity Inspector tab, set the class to the new class that you just created.

You should then be able to CTRL-click drag any UIButtons or other UI elements onto the new .h or .m file and implement any custom behavior for the view there.

If you don't create files to implement your custom classes, you are stuck with the default UI classes like UIViewController, which only have their default behaviors unless you extend them with custom classes.

You generally want one .m and .h file per view controller.

这篇关于为视图控制器设置插座/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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