繁体   English   中英

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

[英]How can I change the image of a button when tapping on another button in another tab

我有一个UITabBarController ,它具有3个选项卡(tab1,tab2,tab3)。 每个选项卡都有1个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从当前类中调用其他类Method,例如下面的示例:-

在您的ViewDidLoad方法中的ViewDidLoad添加通知:-

- (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中,单击“用于调用更新通知的操作”。

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

希望对您有帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM