在代码中将 UIButton 连接到 IBOutlet 和 IBAction [英] Connecting a UIButton to an IBOutlet and IBAction in code
本文介绍了在代码中将 UIButton 连接到 IBOutlet 和 IBAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是viewDidLoad:
I have this is viewDidLoad:
// Set up register button
UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeSystem];
[registerButton setTitle:@"Register" forState:UIControlStateNormal];
registerButton.frame = CGRectMake(0.0, 0.0, self.screenWidth / 2.0, 100.0);
还有这个:
- (IBAction)buttonPressed:(UIButton *)sender
{
}
我的 .h 文件中也有这个:
I also have this in my .h file:
@property (nonatomic, weak) IBOutlet UIButton *registerButton;
如何将 IBOutlets 和 IBActions 连接到我在代码中创建的按钮?
How do I connect IBOutlets and IBActions to buttons I create in code?
推荐答案
你必须像这样为按钮设置一个目标:
You have to set a target to the button like this:
[registerButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
这篇关于在代码中将 UIButton 连接到 IBOutlet 和 IBAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文