如何向自定义视图添加操作?与 UIButton 的操作类似,我可以在 Inteface Builder 中进行连接。我不能使用像下面这样的 SEL,因为它不是一个对象,我该用什么?
 @property(nonatomic, weak) IBOutlet SEL action;
如何向自定义视图添加操作?与 UIButton 的操作类似,我可以在 Inteface Builder 中进行连接。我不能使用像下面这样的 SEL,因为它不是一个对象,我该用什么?
 @property(nonatomic, weak) IBOutlet SEL action;
您想让您的自定义对象成为 UIControl 的子类(它是 UIView 的子类)。然后你得到所有的目标/动作方法......如果你在代码中这样做,那么要探索的将是
– addTarget:action:forControlEvents:
如果您制作自定义 UIControl 对象,则可以在情节提要中拖出自定义视图并将其自定义类设置为您的 customControl。然后,您可以将 IBAction 链接拖到您的 viewController 中,就像它是一个按钮一样。
以下是如何在代码中制作...
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIControl* customControl = [[UIControl alloc]initWithFrame:CGRectMake(20, 20, 200, 200)];
    customControl.backgroundColor = [UIColor redColor];
    [customControl addTarget:self 
                      action:@selector(customAction:) 
            forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:customControl];
}
- (void)customAction:(id)sender
{
    NSLog (@"touched");
}