Ios 在另一个选项卡中单击另一个按钮时,如何更改按钮的图像
我有一个Ios 在另一个选项卡中单击另一个按钮时,如何更改按钮的图像,ios,objective-c,uitableview,uibutton,Ios,Objective C,Uitableview,Uibutton,我有一个UITabBarController,它有3个选项卡(选项卡1、选项卡2、选项卡3)。每个选项卡都有一个UITableView,每个表视图的每行都有一个按钮。现在我希望用户点击tab2上的任何按钮,tab1中的按钮应该已经改变了图像。我可以在tab1中获得按钮的标签,但我不知道当单击tab2中的按钮时如何获得tab1中按钮的标签。我该怎么做 非常感谢 这是我在tab1中创建tableview的代码: - (UITableViewCell *)tableView:(UITableView
UITabBarController
,它有3个选项卡(选项卡1、选项卡2、选项卡3)。每个选项卡都有一个UITableView,每个表视图的每行都有一个按钮。现在我希望用户点击tab2上的任何按钮,tab1中的按钮应该已经改变了图像。我可以在tab1中获得按钮的标签,但我不知道当单击tab2中的按钮时如何获得tab1中按钮的标签。我该怎么做
非常感谢
这是我在tab1中创建tableview的代码:
- (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
类按钮中调用此方法,然后单击用于调用更新通知的操作
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateBtn" object:self];
希望它能帮助您:)如果我想将字符串从tab2发送到tab1?我怎么做?感谢使用一个带有属性和sythsize的全局字符串变量创建到AppDelate类和appdelegate对象的帮助中。您可以使用此项目的任何软件谢谢,但它仍然存在问题,当我单击Tab2中的按钮时,我可以获取密钥,然后我需要将此密钥发送到tab1,如何做到这一点?objappdelegate.strVariable=@“key”;并在第二个tab方法[lblVariable setText:Objappdelgate.strVariable]中使用它;是的,我确实传递了字符串,但现在我不知道如何获取uitableview上的哪个按钮1?我知道uitableview的indexath.row,但不知道检测哪个按钮1。因为每行有2个按钮