简体   繁体   中英

UIButton addTarget:action:forControlEvents: not working using itself as a target?

There is a subclass of UIButton , is it possible to add a target for the button using the button itself? For example

[self addTarget:self action:@selector(onClick:forEvent:) forControlEvents:UIControlEventTouchUpInside];

Yes, simply change this:

[self addTarget:self action:@selector(onClick:forEvent:) forControlEvents:UIControlEventTouchUpInside];

To this:

[MyButton addTarget:self action:@selector(onClick:forEvent:) forControlEvents:UIControlEventTouchUpInside];

Its not working bcoz you are adding self as target. You need to add Your ViewController as target on which you button is added.

[self addTarget:viewController action:@selector(onClick:forEvent:) forControlEvents:UIControlEventTouchUpInside];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM