gpt4 book ai didi

带有 IBAction 的 IOS UIButton

转载 作者:行者123 更新时间:2023-11-29 01:30:00 26 4
gpt4 key购买 nike

我在下面的代码中为 for 循环创建了一些按钮。

UIView *buttonsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _viewEmoji.frame.size.width, 35)];
[buttonsView setBackgroundColor:[UIColor greenColor]];
for (int i = 0; i < myObject.count; i ++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(35*i + 5, 0, 35, 35)];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", [myObject objectAtIndex:i]]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[buttonsView addSubview:button];
}

现在,如何单击按钮来处理事件。

每点击一个按钮,都会处理1个事件。

例如:如果我有 2 个按钮是由 for 循环创建的。当我点击按钮 1 时,它会记录 1,当我点击按钮 2 时,它会记录 2。

最佳答案

喜欢

UIView *buttonsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _viewEmoji.frame.size.width, 35)];
[buttonsView setBackgroundColor:[UIColor greenColor]];
for (int i = 0; i < myObject.count; i ++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(35*i + 5, 0, 35, 35)];
[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", [myObject objectAtIndex:i]]] forState:UIControlStateNormal];
[button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
// set the tag for identify which button you pressed
button.tag = i; // else use --> [myObject objectAtIndex:i]
[buttonsView addSubview:button];
}

//here add your buttonsView to mainview
self.view addsubview(buttonsView)

// for button action
-(void)clickButton:(UIButton*)sender
{
NSLog(@" Index: %d ", sender.tag);
[sender setBackgroundImage:[UIImage imageNamed:@"XXX.png"] forState:UIControlStateNormal];

}

关于带有 IBAction 的 IOS UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33548622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com