3D Touch快捷菜单不会重定向到视图控制器

时间:2017-06-05 00:31:36

标签: ios objective-c 3dtouch

我正在使用objective-c创建3D触控快捷菜单。我已经按照这个例子https://github.com/dakeshi/3D_Touch_HomeQuickAction了解了如何做到这一点,并查看了如何实现它的快速教程https://www.youtube.com/watch?v=NO9E5KxixOw,但仍然存在问题。

快捷菜单显示完美,但是点击其中一个项目后,它会将我带到应用程序的主页,而不是我指定的视图。

以下是我的app delegate中使用的代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Check API availiability
// UIApplicationShortcutItem is available in iOS 9 or later.
if([[UIApplicationShortcutItem class] respondsToSelector:@selector(new)]){

    [self configDynamicShortcutItems];

    // If a shortcut was launched, display its information and take the appropriate action
    UIApplicationShortcutItem *shortcutItem = [launchOptions objectForKeyedSubscript:UIApplicationLaunchOptionsShortcutItemKey];

    if(shortcutItem)
    {
        // When the app launch at first time, this block can not called.

        [self handleShortCutItem:shortcutItem];

        // This will block "performActionForShortcutItem:completionHandler" from being called.
        shouldPerformAdditionalDelegateHandling = NO;


    }
}
 return shouldPerformAdditionalDelegateHandling;

- (void)configDynamicShortcutItems {

// config image shortcut items
// if you want to use custom image in app bundles, use iconWithTemplateImageName method
UIApplicationShortcutIcon *shortcutSearchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeBookmark];
UIApplicationShortcutIcon *shortcutFavoriteIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeBookmark];

UIApplicationShortcutItem *shortcutSearch = [[UIApplicationShortcutItem alloc]
                                             initWithType:@"com.sarangbang.QuickAction.Search"
                                             localizedTitle:@"Events"
                                             localizedSubtitle:nil
                                             icon:shortcutSearchIcon
                                             userInfo:nil];

UIApplicationShortcutItem *shortcutFavorite = [[UIApplicationShortcutItem alloc]
                                               initWithType:@"com.sarangbang.QuickAction.Favorite"
                                               localizedTitle:@"Latest News"
                                               localizedSubtitle:nil
                                               icon:shortcutFavoriteIcon
                                               userInfo:nil];


// add all items to an array
NSArray *items = @[shortcutSearch, shortcutFavorite];

// add the array to our app
[UIApplication sharedApplication].shortcutItems = items;
}

- (BOOL)handleShortCutItem : (UIApplicationShortcutItem *)shortcutItem{

    BOOL handled = NO;

    NSString *bundleId = [NSBundle mainBundle].bundleIdentifier;

    NSString *shortcutSearch = [NSString stringWithFormat:@"%@.Search", bundleId];
    NSString *shortcutFavorite = [NSString stringWithFormat:@"%@.Favorite", bundleId];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];


    if ([shortcutItem.type isEqualToString:shortcutSearch]) {
        handled = YES;

        EventsViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"EventsViewController"];

        UINavigationController *navController;
[navController pushViewController:vc animated:YES];

        self.window.rootViewController = vc;

        [self.window makeKeyAndVisible];

    }

    else if ([shortcutItem.type isEqualToString:shortcutFavorite]) {
        handled = YES;

        NewsViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"NewsViewController"];

        self.window.rootViewController = vc;

        [self.window makeKeyAndVisible];
    }


    return handled;
}


- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{

BOOL handledShortCutItem = [self handleShortCutItem:shortcutItem];

completionHandler(handledShortCutItem);
}

0 个答案:

没有答案