如何在另一个选项卡中点击另一个button时更改button的图像

我有一个UITabBarController有3个选项卡(tab1,tab2,tab3)。 每个选项卡有一个UITableView,每个表视图在每一行都有一个button。 现在我想要用户点击tab2上的任何button,并且tab1中的button应该已经改变了图像。 我可以在tab1中获取button的标签,但是当点击tab2中的button时,我不知道如何获得tab1中button的标签。 我怎样才能做到这一点?

非常感谢。

这是我的代码在tab1中创buildtableview:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; UIButton *market= [UIButton buttonWithType:UIButtonTypeCustom]; [market setFrame:CGRectMake(200, 6, 30, 30)]; market.tag = 4000; [market addTarget:self action:@selector(marketPressedAction:) forControlEvents:UIControlEventTouchDown]; [cell.contentView addSubview:market]; UILabel *pricelabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 80, 30)]; pricelabel.backgroundColor = [UIColor clearColor]; pricelabel.font = [UIFont fontWithName:@"Helvetica" size:16]; pricelabel.font = [UIFont boldSystemFontOfSize:16]; pricelabel.textColor = [UIColor darkGrayColor]; pricelabel.tag = 3000; pricelabel.textAlignment = NSTextAlignmentRight; [cell.contentView addSubview: pricelabel]; [pricelabel release]; } UIButton *marketButton = (UIButton*)[cell.contentView viewWithTag:4000]; [marketButton setTag:indexPath.row]; if([sellingArray count]>0) { if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) // nothing { [marketButton setSelected:NO]; [marketButton setImage:[UIImage imageNamed:@"Marketplace.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) // marketplace { [marketButton setSelected:YES]; [marketButton setImage:[UIImage imageNamed:@"MarketplaceSelect.png"] forState:UIControlStateNormal]; marketButton.enabled = YES; } } [market setTag:indexPath.row]; if([priceNewArray count]> 0) { UILabel *pricelbl = (UILabel*)[cell.contentView viewWithTag:3000]; pricelbl.text =[NSString stringWithFormat:@"$%@",[priceNewArray objectAtIndex:indexPath.row]]; if ([sellingArray count]>0) { if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"2"]) { pricelbl.hidden = NO; } else if([[sellingArray objectAtIndex:indexPath.row] isEqualToString:@"0"]) { pricelbl.hidden = YES; } } } return cell; } 

我尝试使用[tableview reloadData],但它只重新加载UILabel,UIButton不会改变图像,当我比较它们。

你可以使用NSNotificationCenter来调用其他类的方法从当前类如下例:

ViewDidLoad方法的MainClass中添加通知:

 - (void)viewDidLoad { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateBtn:) name:@"updateBtn" object:nil]; [super viewDidLoad]; } -(void)UpdateBtn:(NSNotification *)notification { //Update your button image code here } 

现在你只需要调用这个方法从你的popupView类button点击Action来调用更新通知

  [[NSNotificationCenter defaultCenter] postNotificationName:@"updateBtn" object:self]; 

希望它是帮助你:)