How to navigate to different view controllers base

2019-07-09 23:39发布

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

Here's how my app looks like:

enter image description here

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: enter image description here

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.

enter image description here

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

enter image description here

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.

1条回答
Rolldiameter
2楼-- · 2019-07-10 00:14

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.

查看更多
登录 后发表回答