使用协调器模式+ UITabBarController恢复NSUserActivity

时间:2019-10-04 19:55:13

标签: ios objective-c design-patterns uitabbarcontroller nsuseractivity

如果我使用协调器模式,谁负责restoreUserActivityState? 在我的UITabBarController子类中,我可以直接访问视图控制器,但是我不确定是否应该使用负责的协调器进行连接。使用协调器,我需要先将NSUserActivity传递给协调器,然后再将其从协调器传递给负责的视图控制器。 什么是干净的方法?

AppDelegate.m

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler
{
    if ([userActivity.activityType isEqualToString:CSSearchableItemActionType]) {
        UITabBarController *tabBarController = (__kindof UITabBarController *)self.window.rootViewController;
        restorationHandler(@[tabBarController]);
    }
    return YES;
}

UITabBarController.m(子类)

// ------------  ------------  ------------  ------------  ------------  ------------
#pragma mark - UIUserActivityRestoring

- (void)restoreUserActivityState:(NSUserActivity *)activity
{
    [self setSelectedViewController:self.mapCoordinator.presenter];
    UIViewController *viewController = [self.mapCoordinator.presenter.childViewControllers firstObject];
    [viewController restoreUserActivityState:activity];
}

响应视图控制器

// ------------  ------------  ------------  ------------  ------------  ------------
#pragma mark - UIUserActivityRestoring

- (void)restoreUserActivityState:(NSUserActivity *)userActivity
{
    NSString *stationObjectIDURI = userActivity.userInfo[CSSearchableItemActivityIdentifier];
    NSURL *URL = [NSURL URLWithString:stationObjectIDURI];
    Station *station = [Station stationWithURIRepresentation:URL inMoc:[self context]];
    if (station) {
        [self.mapView updateRegionForCoord:station.coordinate];
        [self.mapView setRegionChangeLock:YES];
        [self.mapView selectAnnotation:station animated:YES];
    }
}

协调员

- (void)start
{
    StationMapVC *stationMapVC = [StationMapVC instantiate];
    stationMapVC.context = self.context;
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Map", nil)
                                                       image:[UIImage
                                                              imageNamed:@"map_marker"]
                                                         tag:0];
    stationMapVC.tabBarItem = item;
    stationMapVC.coordinator = self;
    self.presenter.navigationBarHidden = YES;
    [self.presenter pushViewController:stationMapVC animated:NO];
    self.stationMapVC = stationMapVC;
}

0 个答案:

没有答案