2016-06-13 38 views
0

我试图使用聚光灯结果来引导用户到我的应用程序中的相应部分。iOS:如何推送到DetailView

MainView有几个按钮允许去应用程序的不同区域,其中一个是电话簿,它在它自己的SplitViewController里面。 MasterView的用户列表为TableViewDetailView包含项目详细信息。

目前,我在我的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; 
} 

这是我的结构,希望它能很好地说明它。

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

我想要的是提供DetailView与用户的信息。在显示MasterViewDetailView的设备上,我希望MasterView看起来好像该行已被点击。

我能做到这一点的最佳方式是什么?如何将uid传递给MasterView,然后模仿水龙头模拟相应行上的触摸。

注:我在MasterView数据是建立为Arrays一个Dictionary,因为在如何通过UID找到正确的用户重要的情况下。

如果您需要更多信息,请让我知道。

回答

1

MasterViewController.h

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

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"); 
    } 
} 

这已经工作了非常适合我要我的大部分项目。

+0

太棒了!谢谢。但是,我怎么称呼这个方法呢? 'AppDelegate'? 'navController'引用'SplitViewController'。当我尝试使用'PhoneDirectoryMasterViewController * master = [[navController viewControllers] firstObject]; [master selectRowWithId:uid];'presentViewController'后面我得到一个错误 - [UINavigationController selectRowWithId:]:无法识别的选择器。 – RoLYroLLs

+0

好吧,我试过这个,似乎工作,但它是获得主视图的正确方法? 'PhoneDirectoryMasterViewController * master = [[[[[navController viewControllers] firstObject] childViewControllers] firstObject];' – RoLYroLLs

+0

@RoLYroLLs是的,您需要通过遍历到'UISplitViewController'层次结构来访问'masterViewController'。作为解决方案的工作原理,请您将其标记为正确的答案?这对其他开发者可能会有所帮助。 –