0

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

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

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

4

1 回答 1

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
  }
}
于 2013-03-18T18:05:23.717 回答