如何在用户点击完成按钮后更改UIButton的图像是iOS中的另一个UIView

时间:2013-08-15 06:54:36

标签: ios objective-c uibutton

当用户点击tableview上的UIButton(tableview在每行中有多个按钮)时,会显示另一个View。 我想在用户点击其他UIView的完成按钮后更改此按钮的图像。我怎样才能做到这一点?我是新手。你能为我提供一些代码吗?提前谢谢。

更新代码:

桌面视图代码:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
        [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];

        [market setTag:3000];
        [market setFrame:CGRectMake(200, 6, 30, 30)];
        [cell.contentView addSubview:market];

    }

   for (UIButton *button in [cell subviews]) { // change name of table here
    if ([button isKindOfClass:[UIButton class]]) {
        button.tag = indexPath.row;
        [button setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];
    }
}


    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);

    return cell;
}

“打开代码”按钮(用户单击以显示另一个视图的按钮)

    - (void)marketPressedAction:(id)sender
{

    UIButton *button = (UIButton *)sender;
    buttontag = button.tag;
    NSLog(@"Market button click at row %d",buttontag);
}

完成按钮的代码:

 -(void)submitMarket
{
    for (UIButton *button in [_tableView subviews]) { // change name of table here
        if ([button isKindOfClass:[UIButton class]]) {
            if (button.tag == buttontag) {
                [button setBackgroundImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal];
            } 
        } 
    }
}

3 个答案:

答案 0 :(得分:1)

试试这个:

[yourButton setBackgroundImage:someImge forState:UIControlStateNormal];

按钮上的代码点击:

 savedTag = button.tag;

完成代码按钮点击:

for (UIButton *button in [table subviews]) { // change name of table here 
  if ([button isKindOfClass:[UIButton class]]) { 
    if (button.tag == savedtag) { 
       [button setBackgroundImage:someImge forState:UIControlStateNormal];
    } 
  } 
}

在cellForRowAtIndexPath中代替:[market setTag:3000];[market setTag:indexPath.row];

试试这个:

替换:

marketButton = (UIButton *)[cell.contentView viewWithTag:3000];
[marketButton setTag:indexPath.row];

有了这个:

for (UIButton *button in [cell subviews]) { // change name of table here
     if ([button isKindOfClass:[UIButton class]]) {
         button.tag == indexPath.row; 
     }
}

还有一件事:将第一行cellForRowAtIndexPath设为:

NSString *CellIdentifier = @"tableCell";

将您的cellForRowAtIndexPath更改为:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        UIButton *market = [UIButton buttonWithType:UIButtonTypeCustom];
        [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown];

        [market setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal];

        [market setTag:indexPath.row];
        [market setFrame:CGRectMake(200, 6, 30, 30)];
        [cell.contentView addSubview:market];

    }

    else {

       for (UIButton *button in [cell subviews]) { // change name of table here
         if ([button isKindOfClass:[UIButton class]]) {
            button.tag = indexPath.row;
         }
    }
}


    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);

    return cell;
}

答案 1 :(得分:0)

您可以为按钮的选定状态设置另一个图像,然后点击完成按钮后只需设置所选状态按钮。

  [yourButton setBackgroundImage:someImge forState:UIControlStateSelected];
完成按钮后

  [yourButton setSelected:YES];

或者,如果您在UIControlStateSelected之前使用,您也可以使用任何其他州。或者甚至是用户说明 - UIControlStateApplication

答案 2 :(得分:0)

您可以使用委托或NSNotificationCenter, 在您的视图中,在完成按钮的位置,您需要创建一个属性

  @property (weak, nonatomic) <protocol_name> delegate;

并且在DoneButton的操作方法中,您需要向该代理发送消息

[self.delegate method_name];

,另一个视图将被设置为此一个

的委托

`[viewWithDoneButton setDelegate:self];

你需要实现委托方法`