【问题标题】:iOS: How to Push to DetailViewiOS:如何推送到 DetailView
【发布时间】:2016-10-13 09:22:33
【问题描述】:

我正在尝试使用 Spotlight 结果将用户引导到我的应用中的相应部分。

MainView 有几个按钮允许转到应用程序的不同区域,其中一个是电话目录,它位于它自己的SplitViewController 中。 MasterViewTableView 中包含用户列表,DetailView 包含项目详细信息。

目前,我的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 的数据设置为DictionaryArrays,以防通过uid 找到合适的用户很重要>.

如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: ios objective-c iphone segue spotlight


    【解决方案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");
        }
    }
    

    这对我的大部分项目都很有效。

    【讨论】:

    • 太棒了!谢谢。但是我该如何调用这个方法呢? AppDelegate? navController 引用 SplitViewController。当我尝试在presentViewController 之后使用PhoneDirectoryMasterViewController *master = [[navController viewControllers] firstObject]; [master selectRowWithId:uid]; 时,我收到错误-[UINavigationController selectRowWithId:]: unrecognized selector
    • 好的,我试过了,似乎可行,但它是获取主视图的正确方法吗? PhoneDirectoryMasterViewController *master = [[[[navController viewControllers] firstObject] childViewControllers] firstObject];
    • @RoLYroLLs 是的,您需要通过遍历UISplitViewController 层次结构来访问您的masterViewController。由于解决方案有效,请您将其标记为正确答案吗?这可能对其他一些开发人员有所帮助。
    • 是的,我会将其标记为正确答案我只是在等待确保我正确调用了masterViewController。谢谢。
    • 嘿,你能看看我的后续问题:stackoverflow.com/questions/37799181/…。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多