gpt4 book ai didi

iphone - 如何使用标签访问 UIButton 并更改其图像?

转载 作者:行者123 更新时间:2023-12-03 20:19:51 24 4
gpt4 key购买 nike

我需要更改 UIButtons 矩阵上的图像,而我所知道的解决按钮的唯一方法就是标签。但我找不到实际使用这个标识符的方法。这些按钮是在 viewDidLoad 期间以编程方式创建的。

这是创建按钮的代码:

#define N_ROWS  4
#define N_COLS 3

int N_IMG = 0;
for (int a = 0; a < N_COLS; a++) {
for (int j = 0; j < N_ROWS; j++) {


UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(a * 65.0 + 25, j * 65.0 + 15, 10.0, 10.0);
aButton.tag = j + a * N_ROWS + 1;
[aButton setBackgroundColor:[UIColor redColor]];

N_IMG = N_IMG++;
[self.view addSubview:aButton];
number_sorted = 1;

}
}

这是设置图像的代码:

- (IBAction)set_image:(id)sender {

#define N_ROWS 4
#define N_COLS 3

int N_IMG = 0;
for (int a = 0; a < N_COLS; a++) {
for (int j = 0; j < N_ROWS; j++) {
uibutton aButton.tag == (j + a * N_ROWS + 1)
setImage:[UIImage imageNamed:[puzzles objectAtIndex:N_IMG]]
forState:UIControlStateNormal];
N_IMG = N_IMG++;

}
}
}

这是问题开始的代码:uibutton aButton.tag == (j + a * N_ROWS + 1)

我可以让谁来让它工作?

最佳答案

简短回答:

回复:“如何使用标签访问 UIButton...”

UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag];

回复:“...并更改其图像?”

[tmpButton setImage:[UIImage imageNamed:@"MyGreatImage.png"] forState:UIControlStateNormal];

.

长答案:

回复:*“这是问题开始的代码:uibutton aButton.tag == (j + a * N_ROWS + 1)”*

是的,我能看到问题所在。您使用的行用于设置按钮的标签,而不是标签获取按钮。

要从已知标签获取按钮,请执行以下操作:

// define the tag index
int tmpTag = 123;//replace "123" with your own logic, i.e. (j + a * N_ROWS + 1)

// get the button with the given tag
UIButton *tmpButton = (UIButton *)[self.view viewWithTag:tmpTag];

// assign the image
tmpImage = [UIImage imageNamed:@"MyGreatImage.png"];
[tmpButton setImage:tmpImage forState:UIControlStateNormal];

奖励代码:在此阶段,您还可以添加或删除与按钮关联的操作。

//Remove all actions associated the button
[aButton removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];

//Assign a new button action: using the exact selector name ("myMethodName")
[aButton addTarget:self action:@selector(myMethodName:) forControlEvents:UIControlEventTouchUpInside];

//Assign a new button action: using a calculated selector name
//(suppose I have a bunch of methods with the prefix "myNumberedMethodName_" followed by an index.
int tmpSomeNumber = 12;
SEL tmpSelector = NSSelectorFromString ([NSString stringWithFormat:@"myNumberedMethodName_%i:",tmpSomeNumber);
// don't forget to include the ":" symbol in the selector name
[aButton addTarget:self action:tmpSelector forControlEvents:UIControlEventTouchUpInside];

注意:“viewWithTag”通常返回一个 View 对象。按钮是一种特殊类型的 View ,具有图像等特殊属性。因此,为了让它返回一个 Button 对象而不是更通用的 View 对象,我们通过在定义中使用 (UIButton *) 将其强制转换来将其初始化为 Button。

但是,如果您只想更改按钮的不透明度,则不必将其转换为按钮。您可以简单地将其初始化为通用 View :

// set opacity of a button
float tmpOpacity = 0.5;//half-visible
UIView *tmpView = [self.view viewWithTag:tmpTag];//get the view object associated with button's tag (remember, a button IS a view)
[[tmpView layer] setOpacity:tmpOpacity];

另请参阅Button with Tag .

关于iphone - 如何使用标签访问 UIButton 并更改其图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2472228/

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