UIButton Action没有被调用

时间:2013-11-22 10:23:41

标签: ios objective-c uibutton

我在A类的子视图中添加了一个按钮,并希望在B类按下的按钮中调用该方法。

A类:

INI-方法:

- (id)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action

INI-Method中的addTarget:

[_playButton addTarget:target
                action:action
 forControlEvents:UIControlEventTouchUpInside];

B组:

调用initMethod

photoView = [[PhotoView alloc] initWithFrame:frame target:self action:@selector(playButtonPressed:)];

操作 - 方法

-(void)playButtonPressed:(id)sender{
NSLog(@"Play Button");
}

你能告诉我为什么我会看到UIButton,但触摸没有反应。

完整的INI方法:

- (id)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
self = [super initWithFrame:frame];

self.userInteractionEnabled = YES;
self.clipsToBounds = YES;
self.contentMode = UIViewContentModeCenter;
// self.contentSize = CGSizeMake(frame.size.width, frame.size.height);

// create the image view
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.userInteractionEnabled = NO;
_playButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
[_playButton addTarget:target
                action:action
 forControlEvents:UIControlEventTouchUpInside];
[_playButton setTitle:@"PLAY" forState:UIControlStateNormal];
_playButton.center = CGPointMake(160.0, 200.0);// for center
[_imageView addSubview:_playButton];

self.userInteractionEnabled = NO;
[self addSubview:_imageView];

return self;
}

2 个答案:

答案 0 :(得分:5)

userInteractionEnabled设置为NO将使视图忽略视图或其子视图中的任何触摸。刚刚删除了这一行。


尝试并替换UIControlEventTouchUpInside

的控件事件

当在按钮边界之外检测到修饰时,将触发UIControlEventTouchUpOutside

UIControlEventTouchUpInside用于检测按钮边界内的修饰。

答案 1 :(得分:3)

改变这个:

[_playButton addTarget:target
                action:action
 forControlEvents:UIControlEventTouchUpOutside];

为此:

[_playButton addTarget:target
                action:action
 forControlEvents:UIControlEventTouchUpInside];