Reputation: 99
I have a class CustomView
(subclass of UIView
without xib file) in which I am creating some labels and buttons.I wanted to use this class in one of my another UIViewController
to add those labels and buttons. I am able to add the labels and buttons to my viewController using custom view but if I add some action or event to the button(which is in custom view) , its not working. Please suggest me what should I do to add the action for the button.
//ViewController code
CustomView *slider=[[CustomView alloc]init];
[self.view addSubview:slider];
//CustomView code
toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleButton setTitle:@"" forState:UIControlStateNormal];
toggleButton.userInteractionEnabled=YES;
// add drag listener
[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
// center and size
toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, width, frame.size.height);
toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.0 alpha:0.1];
[toggleButton.layer setBorderWidth:4.0];
[toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
toggleButton.layer.cornerRadius=4.0;
[toggleButton setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
// add it, centered
[self addSubview:toggleButton];
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
NSLog(@"inside drag");
}
Upvotes: 2
Views: 5050
Reputation: 1623
Replace your code with followings:
CustomView.h :
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface CustomView : UIView
@property (nonatomic, strong) UIButton *toggleButton;
@end
CustomView.m :
#import "CustomView.h"
@implementation CustomView
@synthesize toggleButton;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
toggleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[toggleButton setTitle:@"" forState:UIControlStateNormal];
toggleButton.userInteractionEnabled=YES;
// add drag listener
[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
// center and size
toggleButton.frame = CGRectMake(frame.origin.x, frame.origin.y, width, frame.size.height);
toggleButton.backgroundColor=[UIColor colorWithRed:0.1 green:0.1 blue:0.0 alpha:0.1];
[toggleButton.layer setBorderWidth:4.0];
[toggleButton.layer setBorderColor:[[UIColor lightGrayColor] CGColor]];
toggleButton.layer.cornerRadius=4.0;
[toggleButton setTitleColor:[UIColor colorWithRed:0.3 green:0.1 blue:0.4 alpha:1.0] forState:UIControlStateNormal];
// add it, centered
[self addSubview:toggleButton];
}
return self;
}
- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event
{
NSLog(@"inside drag");
}
Add CustomView to the ViewController like:
CustomView *slider=[[CustomView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[self.view addSubview:slider];
I tested this code and it works :)
Upvotes: 2
Reputation: 3399
You added target as self
for the action wasDragged:withEvent
. For this to work(assuming wasDragged:withEvent
is declared in you custom view), you have to rewrite the code to
[toggleButton addTarget:slider action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
Upvotes: 0
Reputation: 2256
Where did you put the method wasDragged:withEvent? because in CustomView code, you set the button target to self, which is customview. So the button TouchDragInside event will be sent to customview not viewController. If you put wasDragged:withEvent in ViewController, it's not gonna work
Upvotes: 0
Reputation: 2451
Replace this line
[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragInside];
with this
[toggleButton addTarget:self action:@selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchUpInside];
Hope this will work
Upvotes: 0