0

AppDelegate クラス内の openURL 関数から ViewController を表示しようとしていますが、運がありません。インターネットで見つけたすべての解決策を試しましたが、何が間違っているのかわかりません...タブ付きのアプリケーションがあることに注意してください..

self.tabBarController.selectedIndex次のようにカスタムビルドのコントローラーを表示したいので、実際には使用したくありません。

CategoryTableViewController *controller = nil;
NSUInteger catId = 6;
NSString *title = @"Cat Title!";
NSManagedObjectContext *inMemoryContext = [xyzclient newContextUsingInMemoryStore:YES];
controller = [[CategoryTableViewController alloc] initWithManagedObjectContext:inMemoryContext];
[(CategoryTableViewController *) controller setParentCategory:catId];
[(CategoryTableViewController *) controller setFilterCategory:NO];
[(CategoryTableViewController *) controller setStopRefresh:YES];
controller.title = title;
[self.tabBarController.selectedViewController.navigationController pushViewController:controller animated:YES];

書かれたコードは次のとおりです。

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    NSString *scheme = [url scheme];

    if ([scheme hasPrefix:@"xyz"]) {
        //Attempt 1
        UINavigationController *searchNavigationController = [self navigationControllerForViewControllerClass:[SearchTableViewController class]];
        [self.tabBarController.selectedViewController.navigationController pushViewController:searchNavigationController animated:YES];

        //Attempt 2

        NSManagedObjectContext *inMemoryContext = [DealsClient newContextUsingInMemoryStore:YES];
        SearchTableViewController *controller = [[SearchTableViewController alloc] initWithManagedObjectContext:inMemoryContext];
        [self.tabBarController.selectedViewController.navigationController pushViewController:controller animated:YES];
     }
}

- (UINavigationController *)navigationControllerForViewControllerClass:(Class)viewControllerClass {
    BaseViewController *viewController = [[viewControllerClass alloc] init];
    viewController.context = [self managedObjectContext];
    UINib *nib = [UINib nibWithNibName:@"BaseNavigationController" bundle:nil];
    UINavigationController *navigationController = [[nib instantiateWithOwner:nil options:nil] lastObject];
    navigationController.viewControllers = [NSArray arrayWithObject:viewController];

    return navigationController;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [super application:application didFinishLaunchingWithOptions:launchOptions];

    self.window.rootViewController = self.tabBarController;

    [DClient setBaseManagedObjectContext:[self managedObjectContext]];

    UINavigationController *featuredNavigationController = [self navigationControllerForViewControllerClass:[FeaturedTableViewController class]];
    featuredNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Home", nil) image:[UIImage imageNamed:@"tabbar_home"] tag:TabBarTabHome];
    UINavigationController *browseNavigationController = [self navigationControllerForViewControllerClass:[CategoryTableViewController class]];
    browseNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Browse", nil) image:[UIImage imageNamed:@"tabbar_browse"] tag:TabBarTabBrowse];
    UINavigationController *searchNavigationController = [self navigationControllerForViewControllerClass:[SearchTableViewController class]];
    searchNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Search", nil) image:[UIImage imageNamed:@"tabbar_search"] tag:TabBarTabSearch];
    UINavigationController *messagesNavigationController = [self navigationControllerForViewControllerClass:[MessagesTableViewController class]];
    messagesNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Messages", nil) image:[UIImage imageNamed:@"tabbar_messages"] tag:TabBarTabMessages];
    UINavigationController *cartNavigationController = [self navigationControllerForViewControllerClass:[CartTableViewController class]];
    cartNavigationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"Cart", nil) image:[UIImage imageNamed:@"tabbar_cart"] tag:TabBarTabCart];

    NSArray *viewControllers = [NSArray arrayWithObjects:featuredNavigationController, browseNavigationController, searchNavigationController, messagesNavigationController, cartNavigationController, nil];
    [self.tabBarController setViewControllers:viewControllers animated:NO];

    [xyzEngine setTabBarController:self.tabBarController];

    [self.tabBarController setCartBadgeValue:[xyz numItemsInCart]];
    NSLog(@"Loaded the page....");
    return YES;
}
4

1 に答える 1

1

OK、あなたが何を達成しようとしているのかまだ完全にはわからないので、次のように仮定します。

5 つの項目を持つタブ バー コントローラーがあります。各項目は、特定のクラスを持つナビゲーション コントローラーです。渡された url パラメータに基づいて、タブ バー コントローラapplication:openURL:sourceApplication:annotation:の適切なアイテム (以前に にロード済み) を選択します。application: didFinishLaunchingWithOptions

これが私の試みです:

- (BOOL)application:(UIApplication *)application 
            openURL:(NSURL *)url 
  sourceApplication:(NSString *)sourceApplication 
         annotation:(id)annotation {

    if ([scheme hasPrefix:@"xyz"]) {
        Class navigationControllerClass = [SearchTableViewController class];

        NSUInteger navigationControllerIndex = [self.tabBarController.viewControllers indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
          BOOL found = [obj isKindOfClass:navigationControllerClass];
          if (found) {
            *stop = YES;
          }
          return found;
        }];

        if (NSNotFound != navigationControllerIndex) {
            self.tabBarController.selectedIndex = navigationControllerIndex;
        }
    }
}

テイク 2

あなたの2回の試行では、新しいナビゲーションコントローラーを作成してから、それを何かにプッシュしようとしていますnil(それ以降self.tabBarController.selectedViewController.navigationControllerはナビゲーションコントローラーです)。nilself.tabBarController.selectedViewController

ナビゲーション コントローラーでプッシュしているものは、別のナビゲーション コントローラーにすることはできません。したがって、タイプの単純なビュー コントローラーを作成し、UIViewControllerそれを現在選択されているタブ バーのナビゲーション コントローラーにプッシュする必要があります。

if ([scheme hasPrefix:@"xyz"]) {
    MYViewController *controller = [[MYViewController alloc] initWithManagedObjectContext:inMemoryContext];
    [(UINavigationController *)self.tabBarController.selectedViewController pushViewController:controller animated:YES];
}
于 2013-05-24T04:53:59.837 に答える