我的应用程序有2个页面(VC),这些都是在BossPageViewController中创建和控制的,我想为用户提供使用快捷方式操作直接进入第二页的选项。现在我知道如何创建快捷方式以及如何触发它们,并且我也知道如何以超级用户身份打开该页面,但是当我以超级用户身份打开该页面时,我无法再滑动到第一页,而且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
}
}
}
}
我也不想将页面实例化为popover或modal或其他任何东西,我只是想要该页面原样而不是该页面的副本。我的意思是复制的代码示例:
let openSecondPage = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "SecondPageVC") as! SecondPageViewController
window?.rootViewController?.present(openSecondPage, animated: false, completion: nil)
我一直在搜索一个月,但没有一个完全像这样的东西,它们很相似,但是他们的代码解决方案不适合我,所以我才发布了这个。
Swift 5.1
好吧,通过对Stackoverflow上发布的不同代码进行混合并针对不同的问题进行反复试验,最后我根据需要为我完成了这项工作,我认为最好共享它,因为我知道有人会需要它,或者可能有更好的方法可以与我们分享的内容。
使用下面的代码触发快捷方式项目,或者如果您想在应用启动时运行它,请在***** func场景(_场景:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.ConnectionOptions)中使用它* ****在sceneDelegate中。
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)
})
}