将选择器添加到UIButton [英] Add Selector to UIButton

查看:150
本文介绍了将选择器添加到UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 ViewController 然后两个不同的 ViewControllers 扩展了主 ViewController ,一个用于iPhone,另一个用于iPad。

I have ViewController and then two different ViewControllers that extend that main ViewController, one for the iPhone and the other for the iPad.

iPad的 ViewController 实例化一个单独扩展 UIView 并将其设置为自己的视图。该视图有一些按钮,我想将它的选择器方法添加为主 ViewController 中的一些方法。如何实现?

The iPad's ViewController instantiates a separate extended UIView and sets it as its own view. That view has some buttons, which I want to add its selector methods as some methods in the main ViewController. How can this be achieved?

所以这里有一种可视化的方法:

So here's a way to visualize this:

Main ViewController
| iPhone ViewController
| iPad ViewController
  | Some UIView Class    --> Button must invoke method in Main View Controller

编辑:
我根本不使用界面构建器。

I do not use interface builder at all.

推荐答案

除非我误解了这个问题,否则没有诀窍。

Unless I'm misunderstanding the question, there's no trick to it.

如果在主视图控制器中定义(IBAction)buttonPressed:(id)sender ,它将由iPad视图继承控制器子类。因此,当iPad视图控制器设置其按钮时,将其指定为属于 self 的选择器应该可以正常工作。

If you define (IBAction)buttonPressed:(id)sender in the main view controller, it's inherited by the iPad view controller subclass. Therefore specifying it as a selector belonging to self when the iPad view controller sets up its button should work fine.

这篇关于将选择器添加到UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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