如何在iphone中选择和取消选择阵列中的缩略图图像和所选图像存储

时间:2013-03-18 17:57:39

标签: iphone ios

我是手机编程的新手。我在里面创建自定义按钮,我为每个自定义按钮附加图像。现在自定义按钮图像显示在缩略图中。如果我选​​择任何缩略图或现在我想要什么自定义按钮。在这里我想选择和取消选择缩略图图像,我想将所选图像标签值存储在数组中。如何做到这一点下面我的代码。使用下面的代码我创建自定义按钮并将图像附加到自定义按钮。

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;
    }

一些身体告诉我,通过使用上面的代码,我会工作,但我没有工作正是我想要的。我想选择和取消选择,并选择我想要存储在数组中的图像。 谢谢 阿斯拉姆

1 个答案:

答案 0 :(得分:3)

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
  }
}