gpt4 book ai didi

ios - 如何在iOS中的按钮数组中检索选定按钮的行和列

转载 作者:行者123 更新时间:2023-12-01 19:25:04 25 4
gpt4 key购买 nike

假设我使用以下代码创建了一个二维按钮数组:

for (NSInteger rowIndex = 0; rowIndex < 6; rowIndex++) 
{
NSMutableArray *rowOfButtons = [[NSMutableArray alloc] init];
for (NSInteger colIndex = 0; colIndex < 7; colIndex++)
{
CGRect newFrame = CGRectMake(2+colIndex * 45, 100 + rowIndex * 40, 45, 40);

UIButton *calButton = [UIButton buttonWithType:UIButtonTypeCustom];
calButton.frame = newFrame;
[calButton setBackgroundColor:[UIColor whiteColor]];

[rowOfButtons addObject:calButton];

[calButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:calButton];
}
[m_button2DArray addObject:rowOfButtons];
}
如何找到该网格内任何位置的单击按钮的行和列?

最佳答案

为每个按钮设置标签,如下所示。

for (NSInteger rowIndex = 0; rowIndex < 6; rowIndex++) 
{
NSMutableArray *rowOfButtons = [[NSMutableArray alloc] init];
for (NSInteger colIndex = 0; colIndex < 7; colIndex++)
{
CGRect newFrame = CGRectMake(2+colIndex * 45, 100 + rowIndex * 40, 45, 40);

UIButton *calButton = [UIButton buttonWithType:UIButtonTypeCustom];
calButton.frame = newFrame;
[calButton setBackgroundColor:[UIColor whiteColor]];

NSInteger tag = (rowIndex * 10) + colIndex;
calButton.tag = tag;


[calButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

[rowOfButtons addObject:calButton];

[self.view addSubview:calButton];
}
[m_button2DArray addObject:rowOfButtons];
[rowOfButtons release];
}

在buttonPressed:方法中,找到以下方法按下了哪个按钮。
-(void)buttonPressed:(id)sender
{
NSInteger tag = [sender tag];
int row = tag / 10;
int col = tag % 10;
//do what you required for the particular button
}

关于ios - 如何在iOS中的按钮数组中检索选定按钮的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8163128/

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