【问题标题】:How can I change the image of a button when tapping on another button in another tab点击另一个选项卡中的另一个按钮时如何更改按钮的图像
【发布时间】:2013-08-21 08:07:57
【问题描述】:

我有一个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 不会改变图像。

【问题讨论】:

    标签: ios objective-c uitableview uibutton


    【解决方案1】:

    您可以使用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类按钮点击Action调用更新通知

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

    希望对你有帮助:)

    【讨论】:

    • 如果我想将字符串从 tab2 发送到 tab1?我该怎么做?谢谢
    • 使用一个具有属性和 sythsize 的 Globle 字符串变量在您的 Appdeleate 类中创建并帮助您使用 appdelegate 的对象,您可以在任何项目中使用它
    • 谢谢,但还是有问题,当我点击Tab2中的按钮时,我可以获得密钥,然后我需要将此密钥发送到tab1,怎么做?
    • objappdelegate.strVariable=@"Key";并在第二个选项卡方法中使用它 [lblVariable setText:Objappdelgate.strVariable];
    • 是的,我确实传递了字符串,但现在我不知道如何获取 uitableview 上的哪个 button1?我知道 uitableview 的 IndexPath.row,但我不知道要检测哪个 button1。因为在每一行都有 2 个按钮
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多