我的应用程序有 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)
我已经搜索了一个月,但没有完全一样的东西,它们很相似,但他们的代码解决方案不适合我,这就是我发布这个的原因。