[英]UIButton target action not being called
Forgive me if this is trivial, I am still honing my programing skills. 如果这很简单,请原谅我,我仍然在磨练我的编程技巧。 I am trying to set this button as a target. 我正在尝试将此按钮设置为目标。 Should be easy but I dont know why it's not working! 应该很容易,但是我不知道为什么它不起作用! I inserted a NSLog to test and the method is not being called! 我插入了NSLog进行测试,但未调用该方法! Thanks for your help. 谢谢你的帮助。
//ShareView.h
@property (strong,nonatomic) UIButton *cancelBtn;
//ShareView.m
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *shareImage = [UIImage imageNamed:@"shareBox.png"];
[self setFrame:CGRectMake(10, 170, shareImage.size.width, shareImage.size.height)];
self.shareIV = [[UIImageView alloc] initWithImage:shareImage];
self.shareIV.userInteractionEnabled = YES;
[self addSubview:self.shareIV];
[self shareBtnsInit];
[self.shareIV addSubview:self.cancelBtn];
self.userInteractionEnabled = YES;
}
return self;
}
-(void)shareBtnsInit{
UIImage *cancelImg = [UIImage imageNamed:@"cancel27.png"];
self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.cancelBtn setImage:cancelImg forState:UIControlStateNormal];
[self.cancelBtn setFrame:CGRectMake(277, 3, cancelImg.size.width, cancelImg.size.height)];
}
//MainViewController.m
-(IBAction)settingsButtonPressed:(id)sender{
self.shareVC = [[ShareView alloc] initWithFrame:CGRectMake(0, 0, 0,0)];
[self.view addSubview: self.shareVC];
[self.shareVC.cancelBtn addTarget:self action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];
}
-(IBAction)settingsCancel:(id)sender{
NSLog(@"TEST!!!");
[self.shareVC removeFromSuperview];
}
基于注释,没有调用settingsButtonPressed:
方法,因此从未设置要查找操作的按钮。
Try adding [self.cancelBtn addTarget:self action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];
尝试添加[self.cancelBtn addTarget:self action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];
to your shareBtnsInit method. 到您的shareBtnsInit方法。
Here's more information on making UIButtons programatically: How do I create a basic UIButton programmatically? 以下是有关以编程方式制作UIButton的更多信息: 如何以编程方式创建基本的UIButton?
[self.cancelBtn addTarget:MainViewController action:@selector(settingsCancel:) forControlEvents:UIControlEventTouchUpInside];
In ShareView you have to import class in which you want your event should get triggered. 在ShareView中,您必须导入要触发事件的类。 Here in ShareView you can import MainViewController class and can use it. 在ShareView中,您可以导入MainViewController类并可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.