nir*_*002 -2 objective-c uibutton ios
我在A类的子视图中添加了一个按钮,并希望在B类按下的按钮中调用该方法.
A类:
INI-方法:
- (id)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
Run Code Online (Sandbox Code Playgroud)
INI-Method中的addTarget:
[_playButton addTarget:target
action:action
forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)
B级:
调用initMethod
photoView = [[PhotoView alloc] initWithFrame:frame target:self action:@selector(playButtonPressed:)];
Run Code Online (Sandbox Code Playgroud)
操作 - 方法
-(void)playButtonPressed:(id)sender{
NSLog(@"Play Button");
}
Run Code Online (Sandbox Code Playgroud)
你能告诉我为什么我看到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;
}
Run Code Online (Sandbox Code Playgroud)
设置userInteractionEnabled为NO将使视图忽略视图或其子视图中的任何触摸.刚刚删除了这一行.
尝试并替换控件事件 UIControlEventTouchUpInside
的UIControlEventTouchUpOutside当触摸起来是检测该按钮的范围之外将被触发.
的UIControlEventTouchUpInside用于检测触摸抬起在一侧的按钮的范围.