Author Topic: Deselecting row in a different tableview controller  (Read 6294 times)

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Deselecting row in a different tableview controller
« on: July 06, 2014, 04:31:10 AM »
I have two tableview controllers (functioning as menus) that are children of a tab bar viewcontroller. I currently have them set up so that a selected row will remain selected until a different row within the same tableview controller is selected by the user. My problem is that I would like a previously selected row in the first tableview controller to also be deselected when the user uses the tab bar to go to the second tableview controller and selects a row in the second tableview controller and vice versa. I'm not sure that having the tableview controllers become each other's delegates would be the best solution here, especially if I start adding more tableview controllers to the tab bar controller. Would using notifications be appropriate here, and if so how would I best implement that or is there a better solution out there? :o

rtamesis

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Deselecting row in a different tableview controller
« Reply #1 on: July 06, 2014, 03:23:53 PM »
I just figured it out using notifications. My only other question then would be where would be the best place to put the [[NSNotificationCenter defaultCenter] removeObserver:self] in these tableviewcontrollers? Would it be proper to place it in the dealloc method? Placing it in the viewDidDisappear: undid all my hard work using notifications.
« Last Edit: July 06, 2014, 03:39:28 PM by rtamesis »

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
Re: Deselecting row in a different tableview controller
« Reply #2 on: July 06, 2014, 04:03:47 PM »
Actually, I think it's simpler to just set your AppDelegate as a delegate of the tab bar controller. To do this:

1. Implement the UITabBarControllerDelegate on your AppDelegate class:

Code: [Select]
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

2. Register the AppDelegate as a delegate for the tab controller:

Code: [Select]
- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    tabBarController.delegate = self;
    [window addSubview:tabBarController.view];
}

3. You can then implement the tabBarController:didSelectViewController: method on the AppDelegate and deselect the currently selected row in the tableview.

Quote
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

All the best!
Kevin
« Last Edit: July 06, 2014, 10:20:50 PM by kjmcneish »