3

如何将函数锚定到我以这种方式创建的 Button

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 类似的操作。是否可以?

4

1 回答 1

4

您做对了(假设您的 aMethod 获得 1 个参数 - 触发事件的 ui 控件)。与 IB 中默认连接的动作的唯一区别是,在 IB 中,动作是为UIControlEventTouchUpInside事件设置的。

于 2011-01-14T13:09:54.320 回答