0

I am using John Lluch's SWRevealViewController which works great, except I cannot figure out how to set selectors in the destination view controller as I do in segues for other UIViewControllers.

Has anyone else encountered this problem?

Here is my code:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Set the title of navigation bar by using the menu items
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
    if ([segue.identifier isEqualToString:@"goalSegue"]) {

        if ([segue.destinationViewController respondsToSelector:@selector(setTitleForViewController:)]) {
            // use performSelector:withObject: to send without compiler checking
            // (which is acceptable here because we used introspection to be sure this is okay)

            NSString * titleText = @"Athlete Goals";

            [segue.destinationViewController performSelector:@selector(setTitleForViewController:) withObject:titleText];
        } else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];

    } else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];

}

I have used this code in prepareForSegue in other view controllers. The selector exists in the destination TableViewController, but SWRevealViewController passes over if-statement without executing.

PhillipOReilly
  • 609
  • 12
  • 28

1 Answers1

1

I think the problem lies in the fact that you are trying to run a selector method on a UINavigationController and not the actual TableViewController in which the selector resides. Notice that destViewController is a UINavigationController, which has your TableViewController inside it.

So actually, you can try the following code and see if it works (I've tried this in a sample project using SWRevealViewController and it works)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Set the title of navigation bar by using the menu items
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    UINavigationController *destViewController = (UINavigationController*)segue.destinationViewController;
    YourTableViewController *tableController = (YourTableViewController *)[destViewController childViewControllers].firstObject;
    if ([segue.identifier isEqualToString:@"goalSegue"]) {

         if ([tableController respondsToSelector:@selector(setTitleForViewController:)]) {
              NSString * titleText = @"Athlete Goals";
              [tableController performSelector:@selector(setTitleForViewController:) withObject:titleText];
         } else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];

    } else destViewController.title = [[menuItems objectAtIndex:indexPath.row] capitalizedString];

}

As far as I understand, this may not exactly be a problem with SWRevealViewController.

Hope this helps.

Gurtej Singh
  • 3,244
  • 1
  • 14
  • 27