首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

尝试通过3d触摸应用图标将VC从performActionFor shortcutItem嵌入到NavigationVC中

通过3D触摸应用图标将VC从performActionFor shortcutItem嵌入到NavigationVC中,可以通过以下步骤实现:

  1. 首先,确保你的应用已经启用了3D Touch功能,并且在Info.plist文件中添加了相应的配置。
  2. 在AppDelegate.swift文件中的application(_:performActionFor:completionHandler:)方法中,处理shortcutItem的点击事件。在该方法中,可以通过shortcutItem的type属性来判断用户点击的是哪个shortcutItem。
  3. 根据shortcutItem的type属性,创建对应的视图控制器(VC)。可以根据需要使用Storyboard或者手动创建VC。
  4. 创建NavigationVC,并将上一步创建的VC作为其根视图控制器。
  5. 在AppDelegate.swift文件中的application(_:didFinishLaunchingWithOptions:)方法中,判断应用是通过3D Touch快捷方式启动的。如果是,则将NavigationVC设置为根视图控制器。

下面是一个示例代码:

代码语言:swift
复制
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    if shortcutItem.type == "com.example.shortcutItem1" {
        let vc = ViewController1() // 创建对应的视图控制器
        let navVC = UINavigationController(rootViewController: vc) // 创建NavigationVC,并将VC作为其根视图控制器
        window?.rootViewController = navVC // 将NavigationVC设置为根视图控制器
    } else if shortcutItem.type == "com.example.shortcutItem2" {
        let vc = ViewController2() // 创建对应的视图控制器
        let navVC = UINavigationController(rootViewController: vc) // 创建NavigationVC,并将VC作为其根视图控制器
        window?.rootViewController = navVC // 将NavigationVC设置为根视图控制器
    }
    
    completionHandler(true)
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
        if shortcutItem.type == "com.example.shortcutItem1" {
            let vc = ViewController1() // 创建对应的视图控制器
            let navVC = UINavigationController(rootViewController: vc) // 创建NavigationVC,并将VC作为其根视图控制器
            window?.rootViewController = navVC // 将NavigationVC设置为根视图控制器
        } else if shortcutItem.type == "com.example.shortcutItem2" {
            let vc = ViewController2() // 创建对应的视图控制器
            let navVC = UINavigationController(rootViewController: vc) // 创建NavigationVC,并将VC作为其根视图控制器
            window?.rootViewController = navVC // 将NavigationVC设置为根视图控制器
        }
    }
    
    return true
}

这样,当用户通过3D Touch点击应用图标上的shortcutItem时,对应的视图控制器将会被嵌入到NavigationVC中,并成为应用的当前界面。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券