25994

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

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:)

Recommend

  • how to create custom button on table view cell
  • Creating Buttons by looping through an array
  • How to create UIButton in UIImageView's top left corner and bottom right corner
  • Custom background on UITabBarItem not set on second tap
  • UITableViewCell accessoryView overlap custom button of cell OR how to change accessoryView position?
  • custom UIButton with skewed area in iPhone
  • UIButton subview in UITableViewCell doesn't hide as expected
  • Undeclared identifier “viewDidLoad”
  • Resizing the window according to a variable swift
  • How to add InputTextView and UIButton to Navigation Controller's toolbar in iOS8?
  • LandScape/Portrait size differ iOS
  • how to set auto layout for dynamically created buttons?
  • CALayer not rendering when added after view visible
  • How to track where the right callout accessory view is when it was tapped on
  • Custom view with nib as subview doesn't seem to be loading
  • How to track where the right callout accessory view is when it was tapped on
  • Using UIButton to navigate between views
  • Breakline in UIButton title
  • Why don't added sublayers show up in screenshot?
  • UITableView and UILabel repeating
  • Can I put a UIScrollView inside another UIScrollView
  • How to set UIImageView in the center of screen horizontally or vertically with Swift, but no more St
  • How to stop the horizontal scrolling programmatically?
  • Merge two imageViews into one and save iOS swift
  • String not fitting in label which is in a subview for swift
  • windowScriptObject method not found on Objective-C code
  • NSTimer and updating UI
  • respondsToSelector - not working
  • Angular 2: is styleUrls relative to the current component?
  • UILabel extra spaces before and after text ios
  • Incorrect behaviour when selecting chips in Angular Material
  • In Angular 2 how to get @Input value updated inside component?
  • Simple Angular 2 app gives “Potentially unhandled rejection” error
  • How to click on a link that has a certain content in puppeteer?
  • How to make the tableview response pan gesture in ZUUIRevealController
  • How to replace TouchesBegan with UIGestureRecognizer
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Jquery popup on mouse over of calendar control
  • Observable and ngFor in Angular 2