0

我的应用程序有 2 个页面(VC),它们在 BossPageViewController 中创建和控制,我想为用户提供使用快捷方式操作直接转到第二页的选项。现在我知道如何创建快捷方式以及如何触发它们,我也知道如何以 root 身份打开该页面,但是当我以 root 身份打开它时,我不再可以滑动到第一页,而且 pageDots 也不存在,所以我应该关闭该应用程序并再次运行它以能够在页面之间滑动并查看 pageDots。

下面的代码是第二页快捷方式触发的地方,我在这里复制并过去了整个sceneDelegate,也许有人想测试它:

import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
var shortcutItemManager: UIApplicationShortcutItem?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    if let shortcutItem = connectionOptions.shortcutItem {
        shortcutItemManager = shortcutItem
    }
    guard let _ = (scene as? UIWindowScene) else { return }
}

func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
    shortcutItemManager = shortcutItem
}

//MARK:- Shortcuts are Triggered here.

func sceneDidBecomeActive(_ scene: UIScene) {
    if let shortcutItem = shortcutItemManager {

        if shortcutItem.type == "com.x.appname.openSecondPage" {



            ///- tag: code lines that run secondPage as root VC

            if let windowScene = scene as? UIWindowScene {
               let window = UIWindow(windowScene: windowScene)
             let storyboard = UIStoryboard(name: "Main", bundle: nil)
               window.rootViewController = storyboard.instantiateViewController(withIdentifier: "SecondPageVC")
               self.window = window
               window.makeKeyAndVisible()



            }
            shortcutItemManager = nil
         }

        }  
      }
   }

我也不想将页面实例化为弹出框或模式或其他任何我的意思只是想要该页面原样而不是该页面的副本。我所说的副本的代码示例:

            let openSecondPage = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "SecondPageVC") as! SecondPageViewController
            window?.rootViewController?.present(openSecondPage, animated: false, completion: nil)

我已经搜索了一个月,但没有完全一样的东西,它们很相似,但他们的代码解决方案不适合我,这就是我发布这个的原因。

4

1 回答 1

0

斯威夫特 5.1

好的,通过混合并在 Stackoverflow 上针对不同问题发布的不同代码进行反复试验,最后我按照我的意愿为我工作,我认为最好分享它,因为我知道有人会需要它或者可能有更好的方法来解决这个问题可以与我们分享。

使用下面的代码触发快捷项目,或者如果您想在应用程序启动时运行它,请在 *****func 场景中使用它(_ 场景:UIScene,willConnectTo 会话:UISceneSession,选项 connectionOptions:UIScene.ConnectionOptions)*** ** 在场景委托中。

if let windowScene = scene as? UIWindowScene {


                let window = UIWindow(windowScene: windowScene)
                let bossPageVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "BossPageVCID") as! BossPageViewController

                self.window = window

                UIView.transition(with: window, duration: 0.1, options: .transitionCrossDissolve, animations: {

                    window.rootViewController = bossPageVC
                    window.makeKeyAndVisible()

                }, completion: { completed in
                    // viewControllerPages is a viewController array I created in BossPageViewController and index of 1 means the second page
                    if let secondPage = bossPageVC.viewControllerPages[1]
                        bossPageVC.setViewControllers([secondPage], direction: .forward, animated: true, completion: nil)

                })
            }
于 2020-03-14T17:08:01.693 回答