gpt4 book ai didi

iOS检测UIButton Press Dynamic索引

转载 作者:行者123 更新时间:2023-11-29 10:50:37 29 4
gpt4 key购买 nike

基本上我是在调用 API 来检索慈善机构列表。然后我将所有这些放在一个数组中并动态设置 UIButtons。

然后我允许用户选择慈善机构并显示包含该索引数据的 View 。

我的循环在这里;

for (int i = 0; i < [self.imageArray count]; i++) {

NSDictionary *listRoles = [self.imageArray objectAtIndex:i];

NSString *charityName = [listRoles objectForKey:@"name"];
NSString *charityDescription = [listRoles objectForKey:@"description"];
NSString *charityImage = [listRoles objectForKey:@"image"];

UIImage *pImage=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:charityImage]]];;

UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:pImage forState:UIControlStateNormal];
[button setTag:i];
[self.scrollView addSubview:button];

然后我有一个点击方法;

- (void)buttonClicked:(UIButton*)button
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
CharityProfileViewController *cpvc = [storyboard instantiateViewControllerWithIdentifier:@"CharityProfile"];
[self presentViewController:cpvc animated:YES completion:nil];
}

我如何检索索引,我知道我可以为 UIButton 设置随机标签,但我怎么仍然知道是哪个?

最佳答案

您可以将按钮的标签值设置为

[button setTag:i+1];

然后在按钮操作中

- (void)buttonClicked:(UIButton*)button
{
NSLog(@"Button Tag : %d",button.tag);

NSLog(@"Selected Index Object : %@",[self.imageArray objectAtIndex:button.tag-1]);
}

关于iOS检测UIButton Press Dynamic索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609931/

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