如何向自定义视图添加操作?类似于UIButtons如何操作我可以在Inteface Builder中连接。我不能使用下面的SEL,因为它不是一个对象,我该使用什么?
@property(nonatomic, weak) IBOutlet SEL action;
答案 0 :(得分:1)
您希望使自定义对象成为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");
}