当呈现视图控制器时,导航栏和选项卡栏丢失。

10

我正在使用3D Touch实现主屏幕快捷方式,目前效果不错。然而,目前的问题是当快捷方式带用户到特定的视图控制器时,选项卡栏和导航栏会消失。

这是我的代码:

func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var handled = false

    if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) {
        let rootViewController = window!.rootViewController

        switch shortcutType {
        case .Favourites:
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootController = storyboard.instantiateViewControllerWithIdentifier("favourites") as! FavouritesTableViewController
            rootController.parkPassed = DataManager.sharedInstance.getParkByName(NSUserDefaults.standardUserDefaults().stringForKey("currentPark")!)
            self.window?.rootViewController = rootController
            self.window?.makeKeyAndVisible()

            handled = true
        }
    return handled
}

有人能建议我需要在代码中改变什么吗?

这是右舷布局(FavouritesTableViewController已经被标出):

enter image description here

编辑:

这是我的更新后的代码:

@available(iOS 9.0, *)
func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
    var handled = false

    if let shortcutType = ShortcutType.init(rawValue: shortcutItem.type) {
        switch shortcutType {
        case .Favourites:
            print("favourites")
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let rootController = storyboard.instantiateViewControllerWithIdentifier("favourites") as! FavouritesViewController
            rootController.parkPassed = DataManager.sharedInstance.getParkByName(NSUserDefaults.standardUserDefaults().stringForKey("currentPark")!)

            let root = UIApplication.sharedApplication().delegate as! AppDelegate
            if let navCont = root.window?.rootViewController?.navigationController {
                navCont.presentViewController(rootController, animated: true, completion: nil)
            } else {
                root.window?.rootViewController?.presentViewController(rootController, animated: true, completion: nil)
            }

            root.window?.makeKeyAndVisible()

            handled = true
        }
    }
    return handled
}

1
如果它们丢失了,那么你现在是从窗口根视图呈现它们的。你必须将你的选项卡栏作为根视图,然后在选项卡栏中呈现它们。 - Teja Nandamuri
我该怎么做?我已经更新了我的问题,并附上了故事板布局。 - user3746428
4个回答

2

我希望我的解决方案能对你有所帮助。

首先,你需要设置Storyboard实例。

  let storyboard = UIStoryboard(name: "Main", bundle: nil)

接下来,您需要设置导航开始的位置。

let mynVC = storyboard.instantiateViewControllerWithIdentifier("root") as! UINavigationController

现在您可以设置要出现的视图控制器。

let playVC = storyboard.instantiateViewControllerWithIdentifier("playVC")

所以现在你可以开始工作流程,但请注意,您必须像这样完成。
 self.window?.rootViewController?.presentViewController(rootVC, animated: true, completion: { () -> Void in
            rootVC.pushViewController(playVC, animated: true)
        })

所以你的rootViewController将呈现“rootVC”,之后是playVC。
希望这能帮助大家 :)

这对我仍然导致没有选项卡的呈现... 有什么想法吗?我的视图层次结构如下:根:Tabbar控制器-> Nav控制器-> destinationView。 - Jerland2

2

尝试这样做:

从应用程序委托获取代理:

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

从 storyboard 中获取你要显示的控制器:

Controller *cont=//get the reference from storyboard either using storyboard ID

然后使你的根视图呈现其他控制器:

[appDelegate.window.rootViewController presentViewController:cont animated:YES completion:^{
                            DLog(@"presented your view ON TOP of the  tab bar controller");
                        }];

Swift:

  var appDelegate: AppDelegate = UIApplication.sharedApplication().delegate()
  var cont: Controller = //get the reference form storyboard
  appDelegate.window.rootViewController.presentViewController(cont, animated: true, completion: {    DLog("presented your view ON TOP of the  tab bar controller")

 })

如果需要的话,您可以将正在展示的内容移动到主线程上!


请注意,您的选项卡必须是根视图控制器。 - Teja Nandamuri
这对我仍然导致没有选项卡的呈现... 有什么想法吗?我的视图层次结构如下:根:Tabbar控制器-> Nav控制器-> destinationView。 - Jerland2

0
如果您像这样使用它,您将会用这个视图控制器替换rootview。因此,这将是一个新的独立页面,通常不会有导航控制器或选项卡控制器。因为您只有这个新页面在视图层次结构中。 如果您想从rootview呈现它,可以尝试一下。
 let root=UIApplication.sharedApplication().delegate as! AppDelegate
    if let navCont=root.window?.rootViewController?.navigationController
    {
        navCont.presentViewController(viewControllerToPresent: UIViewController, animated: true, completion: nil)
    }
    else{
        root.window?.rootViewController?.presentViewController(viewControllerToPresent: UIViewController, animated: true, completion: nil)
    }

哦!也可以使用 root.window?.makeKeyAndVisible()。我在我的一个旧项目中使用过这个方法。它是可行的。 - kocakmstf
它仍然给我警告。我已经发布了我的更新代码,所以你可以检查一下。 - user3746428

0
问题在于,您正在将视图控制器(命名为:rootController)设置为window?.rootViewController,而不是通过window?.rootViewController呈现rootController。只需更改即可。
self.window?.rootViewController = rootController

self.window?.rootViewController.presentViewController(rootController, animated: true, completion: nil)

啊,那就有道理了。但是它给我的输出是:警告:试图在不在窗口层次结构中的 <UINavigationController:0x1500db800> 视图上呈现 <Alton_Towers_Times.FavouritesViewController:0x14ff1b060>! - user3746428

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接