2016-06-13 69 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'。作爲解決方案的工作原理,請您將其標記爲正確的答案?這對其他開發者可能會有所幫助。 –