以编程方式将函数锚定到 UIButton
如何将函数锚定到我以这种方式创建的按钮,
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(80.0, 160.0, 160.0, 40.0);
[button setTitle:string forState:UIControlStateNormal];
[self.view addSubview:button];
我希望在将函数链接到按钮时执行与 IB 类似的操作。 是否可以?
How can I anchor a function to my Button which I created this way
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize: 12];
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(80.0, 160.0, 160.0, 40.0);
[button setTitle:string forState:UIControlStateNormal];
[self.view addSubview:button];
I want to do similar as it do IB when I link a function to a button.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你做得对(假设你的 aMethod 获得 1 个参数 - 触发事件的 ui 控件)。与 IB 中默认连接的操作的唯一区别是,在 IB 中操作是为
UIControlEventTouchUpInside
事件设置的。You're doing it right (assuming your aMethod gets 1 parameter - the ui control that triggers event). The only difference from action wired by default in IB - is that in IB action is set for
UIControlEventTouchUpInside
event.