单击

时间:2015-04-22 12:43:40

标签: ios objective-c xib

在我的单元格中,我有3个按钮,当用户点击每个按钮时,会触发以下3个方法。

enter image description here

- (void) but1:(id) sender{
    NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);

}

- (void) but2:(id) sender{
    NSLog(@"Touched 2 %ld",(long)[(UIButton *)sender tag]);

}

- (void) but3:(id) sender{
    NSLog(@"Touched 3 %ld",(long)[(UIButton *)sender tag]);

}

想象一下,用户点击按钮1,然后but1方法应该被触发,按钮1的背景图像也应该改变。如何在上述方法中进行此更改。

注意:我使用的是XIB个文件。

我能够访问包含该单元格的NIB文件。

UINib *cellNib = [UINib nibWithNibName:@"MyCell" bundle:nil];
        [self.contextMenuTableView registerNib:cellNib forCellReuseIdentifier:@"cell"];

现在我如何访问他触摸的按钮并更改图像?

更新

这是一个表格视图。在每个单元格中有3个按钮,我应该可以选择并取消选择表格中的其中一个按钮。

例如,如果单元格中只能选择1个按钮。选择单元格后,我将显示图像。当它不是我将显示默认图像。

enter image description here

6 个答案:

答案 0 :(得分:2)

您可以将发件人的图像更改为

- (void) but1:(id) sender{
    NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);
    [((UIButton *)sender) setImage:[UIImage imageNamed:"image.png"] forState:UIControlStateNormal]
}
祝你好运

答案 1 :(得分:1)

尝试类似

的内容
          UIImage *btnImage = [UIImage imageNamed:@"image.png"];
          [btnTwo setImage:btnImage forState:UIControlStateNormal];

答案 2 :(得分:0)

要知道按下哪个按钮,您可以为每个按钮设置标记。从该标签,您将获得录制的按钮

在界面部分..

int count;

在viewDidLoad

计数= 0;

mongoimport

答案 3 :(得分:0)

- (void) but1:(id) sender{

  [but1 setBackgroundImage:[UIImage imageNamed:@"blue_button.png"]
                            forState:UIControlStateNormal];
  NSLog(@"Touched 1 %ld",(long)[(UIButton *)sender tag]);

 }

试试这段代码,这可能会对你有所帮助

答案 4 :(得分:0)

可以通过这种方式存档,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UIButton *btn1 = (UIButton *) [cell viewWithTag:1] ;
    UIButton *btn2 = (UIButton *) [cell viewWithTag:2];
    UIButton *btn3 = (UIButton *) [cell viewWithTag:3];
    [btn1 setTag:((indexPath.row*10)+1)];
    [btn1  setTag:((indexPath.row*10)+2)];
    [btn3 setTag:((indexPath.row*10)+3)];
    [btn1 addTarget:self action:@selector(but1:) forControlEvents:UIControlEventTouchDown];
    [btn2 addTarget:self action:@selector(but2:) forControlEvents:UIControlEventTouchDown];
    [btn3 addTarget:self action:@selector(but3:) forControlEvents:UIControlEventTouchDown];
    return cell;
}

- (void) but1:(UIButton *) sender{
    NSLog(@"Touched 1 %ld",((long)[(UIButton *)sender tag])%10);
    [sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
    NSInteger row = ((long)[sender tag])/10;
    NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableview beginUpdates];
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableview endUpdates];
}

- (void) but2:(UIButton *) sender{
    NSLog(@"Touched 2 %ld",((long)[(UIButton *)sender tag])%10);
    [sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
    NSInteger row = ((long)[sender tag])/10;
    NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableview beginUpdates];
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableview endUpdates];
}

- (void) but3:(UIButton *) sender{
    NSLog(@"Touched 3 %ld",((long)[(UIButton *)sender tag])%10);
    [sender setBackgroundImage:[UIImage imageNamed:@"newImage.png"] forState:UIControlStateNormal];
    NSInteger row = ((long)[sender tag])/10;
    NSIndexPath *indexPathOfYourCell = [NSIndexPath indexPathForRow:row inSection:0];
    [self.tableview beginUpdates];
    [self.tableview reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPathOfYourCell] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableview endUpdates];
}

享受编码!!

答案 5 :(得分:0)

您也可以使用此

button1.tag = 0;
button2.tag = 1;
button3.tag = 2;
[button1 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button1 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];
[button2 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button2 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];
[button3 setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[button3 setImage:[UIImage imageNamed:@"selected_image.png"] forState:UIControlStateSelected];

向所有按钮添加一个公共选择器(buttonClicked:)并按如下方式实现选择器

- (void)buttonClicked:(id)sender
{
    [button1 setSelected:NO];
    [button2 setSelected:NO];
    [button3 setSelected:NO];

    if ([sender isKindOfClass:[UIButton class]]) 
        [sender setSelected:YES];
}