gpt4 book ai didi

iphone - 如何选择和取消选择缩略图图像和所选图像存储在 iphone 数组中

转载 作者:行者123 更新时间:2023-11-28 20:22:10 26 4
gpt4 key购买 nike

我是手机编程的新手。我在里面创建了自定义按钮,我正在为每个自定义按钮附加图像。现在自定义按钮图像以缩略图显示。现在,如果我选择任何缩略图图像或自定义按钮。在这里我想选择和取消选择缩略图图像,我想将所选图像标签值存储在数组中。如何执行此操作在我的代码下方。使用下面的代码我正在创建自定义按钮并将图像附加到自定义按钮。

blaukypath =[[NSMutableArray alloc]init];
for (NSString* path in array)
{
[blaukypath addObject:[UIImage imageWithContentsOfFile:path]];
NSLog(@"%@",path);
}
myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0, 0.0, 320.0, 840.0)];
myScrollView.delegate = self;
myScrollView.contentSize = CGSizeMake(320.0, 840.0);
myScrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:myScrollView];

float horizontal = 8.0;
float vertical = 8.0;
for(int i=0; i<[blaukypath count]; i++)
{
if((i%4) == 0 && i!=0)
{
horizontal = 8.0;
vertical = vertical + 70.0 + 8.0;
}

buttonImage = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonImage setFrame:CGRectMake(horizontal, vertical, 70.0, 70.0)];
[buttonImage setTag:i];

[buttonImage setImage:[blaukypath objectAtIndex:i] forState:UIControlStateNormal];
[buttonImage addTarget:self action:@selector(buttonImagePressed:) forControlEvents:UIControlEventTouchUpInside];
[myScrollView addSubview:buttonImage];

horizontal = horizontal + 70.0 + 8.0;
}

[myScrollView setContentSize:CGSizeMake(320.0, vertical + 78.0)];


[self.myScrollView addSubview:buttonImage];

现在,如果选择任何我想选择的缩略图图像,取消选择我想存储在数组中的缩略图图像和选定的缩略图图像。

-(void)buttonImagePressed:(id)sender
{
UIButton *btn = (UIButton*)sender;

if (btn.tag==0)
{
[btn setImage:[UIImage imageNamed:@"Default.png"] forState:UIControlStateNormal];
btn.tag=1;
}
else{
[btn setImage:nil forState:UIControlStateNormal];
btn.tag=0;
}

有些人告诉我,通过使用上面的代码,我会工作,但我并没有完全按照我想要的方式工作。我想选择和取消选择,还选择了我想存储在数组中的图像。谢谢阿斯拉姆

最佳答案

根据UIControlState设置按钮的图像

@property(nonatomic,retain)NSMutableArray *tapCollection;

[btn setImage:[UIImage imageNamed:@"buttonBackGround.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"Button_Selected.jpg"] forState:UIControlStateSelected];

-(void)viewDidLoad{

self.tapCollection = [[NSMutableArray alloc] init];
}

-(void)buttonImagePressed:(id)sender
{
UIButton *selectedButton = (UIButton *)sender;

//If checked, uncheck and visa versa
[selectedButton setSelected:![selectedButton isSelected]];

if([selectedButton isSelected])
{
[self.tapCollection addObject:[NSNumber numberWithInt:btn.tag]];
}
else
{
//remove btn.tag from self.tapCollection
}
}

关于iphone - 如何选择和取消选择缩略图图像和所选图像存储在 iphone 数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15483655/

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