我试图在我的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;
}
答案 0 :(得分:1)
好的,我仍然不完全确定你想要达到的目标,所以我会假设以下内容:
您有一个带有5个项目的标签栏控制器。每个项目都是具有特定类的导航控制器。根据传递给application:openURL:sourceApplication:annotation:
的url参数,您要选择标签栏控制器中的正确项目(之前已在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次尝试中,您正在创建一个新的导航控制器,然后尝试将其推送到nil
(self.tabBarController.selectedViewController.navigationController
为nil
的内容,因为{ {1}}是导航控制器。)
无论你在导航控制器中推动什么,都不能成为另一个导航控制器。因此,您需要创建一个类型为self.tabBarController.selectedViewController
的简单视图控制器,并将其推送到选项卡栏的当前所选导航控制器。
UIViewController