0

UITabBarController3 つのタブ (tab1、tab2、tab3) があります。各タブには 1 つの UITableView があり、各テーブル ビューには各行にボタンがあります。ここで、ユーザーに tab2 の任意のボタンをクリックしてもらい、tab1 のボタンで画像が変更されている必要があります。タブ1のボタンのタグを取得できますが、タブ2のボタンをクリックしたときにタブ1のボタンのタグを取得する方法がわかりません。どうやってやるの?

どうもありがとう。

これは、tab1でテーブルビューを作成する私のコードです:

- (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 は画像を変更していません。

4

1 に答える 1

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クラスからボタンをクリックして、更新通知を呼び出すためのアクションを実行します。

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

それがあなたを助けることを願っています:)

于 2013-08-21T08:10:47.520 に答える