我试图从我的 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;
}