RoLYroLLs
RoLYroLLs

Reputation: 3215

iOS: How to Push to DetailView

I'm trying to use spotlight results to guide the user to the appropriate section within my app.

The MainView has a few button allowing to go to different areas of the app, one of them being a phone directory, which is inside it's own SplitViewController. The MasterView has the list of users in a TableView and the DetailView has the item details.

Currently, I have the following in my AppDelegate.m.

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
    if ([userActivity.activityType isEqualToString:CSSearchableItemActionType]) {

        // uid = an Id field unique for each record
        NSString *uid = userActivity.userInfo[CSSearchableItemActivityIdentifier];

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UINavigationController *navController = nil;
        navController = [storyboard instantiateViewControllerWithIdentifier:@"PhoneDirectory"];

        if (navController != nil) {
            [[[self window] rootViewController] presentViewController:navController animated:YES completion:nil];
        }
    }

    return YES;
}

Here is my structure, hopefully it does a good job illustrating it.

Navigation Controller
|
|-- Main View
    |-- About View
    |
    |-- SplitView Controller (StoryboardID: PhoneDirectory)
        |-- Master View Controller
        |-- Detail View Controller
    |--Another View

What I want is to present the DetailView with the information of the user. On devices which show the MasterView and DetailView I would like the MasterView to look as if the row was tapped.

What's the best way i can accomplish this? How do I pass the uid to the MasterView and then mimic a tap to simulate a touch on the appropriate row.

Note: my data at the MasterView is setup as a Dictionary of Arrays, in case that matters in how to find the proper user by uid.

If you need further information, please let me know.

Upvotes: 0

Views: 95

Answers (1)

Tarun Tyagi
Tarun Tyagi

Reputation: 10112

Inside MasterViewController.h

-(void)selectRowWithID:(NSString*)uid;

Inside MasterViewController.m

-(void)selectRowWithID:(NSString*)uid
    {
        NSPredicate* predicate = nil;
        //Your logic to find the match from array;

    NSArray* matches = [dataSourceArray filteredArrayUsingPredicate:predicate];

    if([matches count] > 0)
    {
        NSDictionary* match = matches[0];

        NSIndexPath* targetIndexPath;
        // Your logic to find out the indexPath for this match


        // Ask the table to select the row programatically
        [self.tableView selectRowAtIndexPath:targetIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];

        // Manually forward the event for this, as if user touched the cell
        // SDK won't forward this method call for programmatic cell selections
        [self tableView:self.tableView didSelectRowAtIndexPath:targetIndexPath];
    }
    else
    {
        NSLog(@"'uid' not found in the master table dataSource");
    }
}

This has been working out great for me for most of my projects.

Upvotes: 1

Related Questions