如何根据滑出菜单导航到不同的视图控制器 [英] How to navigate to different view controllers based on slide out menu

查看:21
本文介绍了如何根据滑出菜单导航到不同的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 的新手,并且已经尝试了很多小时.

我的应用程序如下所示:

理想情况下,我希望他们从这个弹出菜单中单击一个选项,将他们带到另一个视图.现在,我在 QuotesTimelineViewController 上,但说我想单击书签按钮,然后我想转到 SavedQuotesViewController.

这是我的故事板的样子:

QuotesViewController 中嵌入了一个 NavigationController.当您单击菜单按钮时,它会转到以模态呈现的 NavigationSideController.现在我不知道如何为我接下来想要完成的事情做转场.

这是我的 NavigationSideController 代码,所以现在当您单击导航侧栏中的图标时会发生什么,它会关闭该动画并且导航侧栏消失.也许你可以看到所有注释掉的代码.

这是 QuotesTimelineViewController 中的一些相关代码(我认为):

但是我可能遗漏了一些东西,所以如果你想看看我的整个 repo,它就在这里:https://github.com/mayaah/ios-decal-proj4

任何指导都会很有帮助!最终我希望每当我点击导航侧栏中的图标时,与该图标对应的视图控制器就会打开.到目前为止,我取得的最大进展是出现窗口层次结构错误!我猜是因为当我点击一个图标时, QuotesTimelineViewController 不在堆栈的顶部或我不知道的东西.

解决方案

我可以在这里提出两个解决方案

  1. 使用侧边菜单第三方库.

<块引用>

在创建应用端时有很多事情需要处理菜单包括嵌套层次结构/旋转、阴影等.第三个派对图书馆将轻松处理它们并经过充分测试代码.你可以从cocoacontrols

中选择你喜欢的

  1. 使用导航控制器并在选择表时更改 Active VC

    基本思想是在您的应用程序委托中创建一个新的 navigationController 并创建所有不同类型的入口点.因此,在您的应用委托中,声明一个新的 UINavigationController 变量.

您不会在这种方法中使用故事板 initialVC.在 didFinishLaunching 应用委托的方法

var navController:UINavigationController!func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) ->布尔{//在应用程序启动后覆盖自定义点.让 storyboard = UIStoryboard(name: Main", bundle: nil)//确保在故事板中为这些 VC 设置故事板 ID让开始VC = storyboard.instantiateViewControllerWithIdentifier(QuotesTimeLine");navController = UINavigationController(rootViewController:startingVC)self.window!.rootViewController = navController返回真}

然后在侧边菜单中,使用新的 VC 更新 navController 的 viewController

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {让 storyboard = UIStoryboard(name: Main", bundle: nil)//确保在故事板中为这些 VC 设置故事板 ID让开始VC = storyboard.instantiateViewControllerWithIdentifier(NewVC");让 appDelegate = UIApplication.sharedApplication().delegate as!应用程序委托appDelegate.navController.viewControllers = [startingVC]解雇视图控制器动画(真,完成:零)}

这仍然很粗糙,但提供了管理层次结构所需的想法.如果需要澄清,请告诉我.

I'm new to Swift and have been trying various thing for many hours now.

Here's how my app looks like:

Ideally I want them to click an option from this pop out menu that will take them to another view. Right now, I'm on the QuotesTimelineViewController but say I want to click on the bookmark button then I'd want to go to the SavedQuotesViewController.

Here is how my storyboard looks like:

The QuotesViewController has a NavigationController embedded in it. When you click the menu button it goes to the NavigationSideController which is presented modally. Now I don't know how to make the segues for what I want to accomplish next.

Here is my code for the NavigationSideController so right now what happens when you click on an icon in the navigationsidebar it dismisses that animation and the navigationsidebar goes away. Maybe you can see all the commented out code.

And here's some relevant code (I think) from QuotesTimelineViewController:

But there could be something I'm missing so if you want to look at my whole repo it's here: https://github.com/mayaah/ios-decal-proj4

Any guidance would be so helpful! Eventually I'd like that whenever I click on an icon in the navigationsidebar the view controller corresponding to that icon opens up. The most progress i've made so far was getting a window hierarchy error! I guess because when I click on an icon, QuotesTimelineViewController isn't the at the top of the stack or something I don't know.

解决方案

I can propose two solutions here

  1. Use a side menu third party library.

There are lot of things to take care of while creating a app side menu including nested heirarchy/rotations, shadows etc. The third party libraries will take care of them easily and are well tested code. You can choose the one you like from cocoacontrols

  1. Use a Navigation Controller and change Active VC on table selection

    The basic idea is to make a new navigationController in your app delegate and make all different type of entry points. so in your app delegate, declare a new UINavigationController variable.

You wont be using the storyboard initialVC in this approach. in didFinishLaunching Method of app delegate

var navController:UINavigationController!

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    // make sure to set storyboard id in storyboard for these VC
    let startingVC =  storyboard.instantiateViewControllerWithIdentifier("QuotesTimeLine");
    navController = UINavigationController(rootViewController: startingVC)
    self.window!.rootViewController = navController
    return true
}

and then in your side menu, you update the navController's viewControllers with new VC

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        // make sure to set storyboard id in storyboard for these VC
        let startingVC =  storyboard.instantiateViewControllerWithIdentifier("NewVC");
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.navController.viewControllers = [startingVC]
        dismissViewControllerAnimated(true, completion: nil)
    }

This is still rough but gives the idea of what you need to manage the heirarchy. Let me know if any clarification is needed.

这篇关于如何根据滑出菜单导航到不同的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆