我正在尝试创建一个按钮,一旦点击将显示另一个UIView的弹出窗口。为了测试这一点,我在viewDidLoad部分中有以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom];
[self.hard1 setFrame:CGRectMake(884, 524, 105, 60)]; // set the x,y,width and height based on your specs
UIImage *buttonImage = [UIImage imageNamed:@"green.jpg"];
hard1.layer.cornerRadius = 10;
hard1.clipsToBounds = YES;
[hard1 addTarget: self
action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
[self.hard1 setImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:self.hard1];
}
并进一步向下:
- (IBAction) buttonClicked: (id)sender
{
NSLog(@"Tap");
}
但是,当我按下按钮时,控制台不会记录“点按”。有什么想法吗?
答案 0 :(得分:12)
观看这三行代码:
hard1.layer.cornerRadius = 10;
hard1.clipsToBounds = YES;
[hard1 addTarget: self
action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
你错过了自我。在所有三个。他们应该是:
self.hard1.layer.cornerRadius = 10;
self.hard1.clipsToBounds = YES;
[self.hard1 addTarget: self
action: @selector(buttonClicked:)
forControlEvents: UIControlEventTouchUpInside];
此外,如果您以编程方式创建它,则它不应该是IBAction(IB代表接口构建器,而不是在接口构建器中创建)。
答案 1 :(得分:3)
self.hard1 = [UIButton buttonWithType:UIButtonTypeCustom];
[hard1 addTarget: self
action: @selector(buttonClicked:) forControlEvents: UIControlEventTouchUpInside];
将hard1替换为self.hard1
答案 2 :(得分:0)
您已将事件响应者设为buttonClicked
,但已在IBAction
中定义了buttonTap
。这显然不起作用......
应该是
[hard1 addTarget: self
action: @selector(buttonTap:)
forControlEvents: UIControlEventTouchUpInside];