我在目标c中使用带有不同标签的for循环创建了按钮,但问题是除了前2-3次按钮点击之外它不会调用BtnClick函数。任何帮助将不胜感激。
for(int i = 0; i<40; i++)
{
UIButton butContinue...
btnContinue.tag=i;
[btnContinue setTitle:[NSString stringWithFormat:@"%d",i]
forState:UIControlStateNormal];
btnContinue.autoresizingMask=YES;
[btnContinue addTarget:self
action:@selector(clickBtn_Continue:)
forControlEvents:UIControlEventTouchUpInside];
}
答案 0 :(得分:0)
请同时动态设置框架。在此代码中,它将在同一帧上添加40个按钮。初始化 带有alloc init的按钮。所以它不会自动释放。使用下面 UIButton * yourButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,50,50)]; //并动态设置框架
答案 1 :(得分:-1)
循环代码以创建按钮
UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[yourButton setFrame:CGRectMake(x, y, 50, 50)];
[yourButton setTitle:@"Click Here" forState:UIControlStateNormal];
[yourButton addTarget:self action:@selector(yourAction:) forControlEvents:UIControlEventTouchUpInside];
yourButton.tag = tag;
[self.view addSubview:yourButton];
//increment x or y as per your need,
行动类似
- (IBAction) yourAction : (UIButton *) sender
{
NSLog(@"I'm %d pressed.",sender.tag);
}