
Question:
I have a UITabBarController
which has 3 tabs (tab1 ,tab2 ,tab3). Each tab has 1 UITableView, and each table view has a button in each row. Now I want the user to click on any button on tab2, and button in tab1 should have changed images. I can get tag of button in tab1 but I don't know how to get tag of button in tab1 when click on button in tab2. How can i do that?
Thanks a lot.
This is my code create tableview in 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;
}
I tried use [tableview reloadData] but it only reloads UILabel, UIButton is not changing the image when I compare them.
Answer1:you can use NSNotificationCenter
for calling other class Method from current class like bellow example:-
add Notification at MainClass in your ViewDidLoad
Method:-
- (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
}
Now you just need to call this method From your popupView
class Button click Action for calling update Notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateBtn" object:self];
Hope it's Helps you:)