Andrew Johnson
Andrew Johnson

Reputation: 13286

Is there a better way to write this working, but extremely ugly, code?

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

  NSLog(@"switching views");
  if([viewController isKindOfClass: [UINavigationController class]] &&
     [[[viewController viewControllers] objectAtIndex: 0] isKindOfClass: [SavedViewController class]]) {

      NSLog(@"its a SavedViewController");
      [[[[viewController viewControllers] objectAtIndex: 0] tableView] reloadData];
  }

}

Upvotes: 0

Views: 441

Answers (3)

Lounges
Lounges

Reputation: 4664

Why not just put the reload data call in the viewDidAppear method of the SavedViewController class?

Upvotes: 2

amattn
amattn

Reputation: 10065

Cocoa's big trade off is readability vs conciseness.

You're not that far off from what I would do:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  NSLog(@"switching views");
  if([viewController isKindOfClass: [UINavigationController class]]) {
      id first_view_controller = [viewController.viewControllers objectAtIndex:0];
      if ([first_view_controller isKindOfClass: [SavedViewController class]) {
          NSLog(@"its a SavedViewController");
          [first_view_controller.tableView reloadData];
      }  
   }
}

Edited: used dot notation in a couple of places per C. McCall

Edited again: looks like ObjC does short circuit.

Upvotes: 1

Chris McCall
Chris McCall

Reputation: 10397

Dot notation would clean up some of the bracket forest, but that's all I can think of.

Upvotes: 1

Related Questions