我正在尝试创建一个按钮,点击后将显示另一个 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");
}
但是,当我按下按钮时,控制台不会记录“Tap”。有什么想法吗?
最佳答案
观察这三行代码:
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 代表界面构建器,它不是在界面构建器中创建的)。
关于iphone - 以编程方式编写 UIButton 操作代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15054379/