Switch between different Views in SWIFT - ios

I read a lot of questions / answers in the stack overflow community about switching between different ViewControllers but I couldn't find the correct answer for my specific problem.
I'm using a NavigationViewController to "connect" all my different viewControllers (ViewController / TableViewController / CollectionViewController)
I have got some classes which aren't depending on a ViewController class. For example to to calculate anything in my "calculate" class.
I'm searching for an easy solution, to switch between all my ViewControllers. And I would like to decide in my "calculate" class e.g. to go one step back in my Navigation ViewController.
In the moment I use this:
Example to switch between two different ViewControllers:
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc = sb.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView
self.presentViewController(vc, animated: true, completion: nil)
PROBLEM: This example only works in a ViewController class and the navigation bar disappears.
Example to go one step back in my NavigationViewController:
self.navigationController?.popViewControllerAnimated(true)
PROBLEM: This example only works in a ViewController class.
EDIT 1:
I found a nice solution to call all my viewControllers with this part of code:
let switchViewController = self.navigationController?.viewControllers[1] as! ScanTableView
self.navigationController?.popToViewController(switchViewController, animated: true)
How can I call this code in a non ViewController class?

Push the new view controller onto your existing navigation stack, rather than presenting it modally:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("StartPage") as! StartPageView
self.navigationController!.pushViewController(vc, animated: true)

Related

Problems using self.navigationController?.pushViewController(newViewController, animated: true)

the thing is im new to ios development this is my second application while working on the first one i mainly used a theme and then worked over it but this one im working from scratch.
My main problem is going from one screen to another
When i use this code it works
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "servicesScreen") as! ServiceViewController
nextViewController.modalPresentationStyle = .fullScreen
self.present(nextViewController, animated:true, completion:nil)
But i need to use this one so i can go back too but this wont do anything
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "servicesScreen") as! ServiceViewController
self.navigationController?.pushViewController(newViewController, animated: true)
My question is, am i doing anything wrong here or do i need to add something in scenedeligate?
try
select Main.storyboard
select initial view Controller
select editor -> Embed in -> navigation controller.
For this, you need to add navigation controller to initial storyboards.
If you are using navigationController you need to add a navigation controller first on storyboard or programmatically.
Programmatically add navigation controller check this link:-
Creating a navigationController programmatically (Swift)
or
To add navigation controller on storyboard you should follow this link:-
https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/ImplementNavigation.html
If you have added navigation controller to your app then make sure your intialViewController is set to navigation controller or your navigation controller is added to the stack when you run your application.
Also check for "Is the identifier correct? Did you reference the correct storyboard?".

Go back previous ViewController from NavigationController in TabViewController

I got the problem is I cannot make go back previous ViewController of different storyboard from NavigationController in TabViewController.
I've already tried with
_ = navigationController?.popViewController(animated: true)
unfortunately, it does not work. And I know that it can be going back with segue but it's not good practice. Please let me know how to do it. Thanks. Following is my hierarchy.
I had the same issue. In this case I am on a different Storyboard and want to return to the Main Storyboard and present the first view controller. Since this controller is embedded in a Navigation Controller that is embedded in a Tab Bar controller, you must instantiate the Tab Bar Controller. Be sure to set the indentifier on the Tab Bar Controller as "TabBarController"... or whatever you like.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController
self.present(controller, animated: true, completion: nil)

Programatically creating Segues in ios swift

In my app I use side bar as in facebook. when the user slides out the side bar a uiimageview is displayed. when user taps on the image it takes hm to a different viewcontroller. the problem i am facing is that I have created sidebar programatically and the other view to which I want to navigate the user is created using storyboard. So my source view is created programatically and destination view is created using storyboard. So can someone explain me if there is any way of using "Segue" in this scenario. Since i can not create segue using storyboard I need to do it programatically but even after a lot of googling i could not find the answer.
Well, to get another instance of another storyboard programmatically you can use something like:
let newController = UIStoryboard(name: "MyStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("MyIdentifier") as! MyViewController
and then you push to your navigation controller, or add as a child view controller or something...
If you don't wanna bother with identifiers you can just use the instantiateInitialViewController instead of instantiateViewControllerWithIdentifier
Might help
"userSB" is the viewcontroller storyboard identifier
#IBAction func tapSearchCriteria(_ sender: Any?) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let aVC = storyboard.instantiateViewController(withIdentifier: "userSB") as? AViewController
aVC?.modalPresentationStyle = UIModalPresentationStyle.custom
aVC?.transitioningDelegate = self
aVC?.udelegate = self
self.present(aVC!, animated: true, completion: nil)
}

iOS Swift, returning to the same instance of a view controller

I have a problem, where I have two view controllers A and B. View controller B has a map, with a route draw on it. I can move back and forwards between the two view controllers at the moment, but the B view controller is reset every time it loads. I think this is because I am using segues and it is creating a new instance of a View controller every time.
I have tried using the following code to solve this issue, but it is still not working. The views load correctly, but view controller B is still being reset
#IBAction func mapButton(sender: AnyObject){
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
What am I doing wrong and how can I fix it? I want view controller B to stay in the memory along with the map and the route, so when a user returns he doesn't have to enter all of the information again.
You should create a variable in your class of type UIViewController and change your code to the following:
#IBAction func mapButton(sender: AnyObject){
if yourVariable == nil {
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
yourVariable = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
}
self.presentViewController(yourVariable, animated: true, completion: nil)
}
That way you create the viewController one time, save it and if you want to open it again present the previously created one.

ios swift uitabbar replace view controllers crashes

I am really struggling with what should be a simple bit of code.
I have an ios app that has 4 tabs in the uitabcontroller, depending on the a setting in another tab I wanted to replace the controller the first tab goes to. I found that I couldnt simply replace this first tab (although i managed it somehow for a couple of builds then it stopped working after a clean).
So I went for the option of replacing the viewcontrollers that the tab controller references with the .setViewControllers method. I call this from my viewcontroller after the viewdidload method.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc0 = storyboard.instantiateViewControllerWithIdentifier("collection")
let vc4 = storyboard.instantiateViewControllerWithIdentifier("profilenews1")
let vc1: UIViewController! = storyboard.instantiateViewControllerWithIdentifier("news") as UIViewController
let vc2: UIViewController! = storyboard.instantiateViewControllerWithIdentifier("create") as UIViewController
let vc3: UIViewController! = storyboard.instantiateViewControllerWithIdentifier("search") as UIViewController
let controllers = [vc0,vc4]
self.tabBarController?.setViewControllers(controllers, animated: true)
From the research I have done this should work, setViewControllers is documented and I have seen numerous objective c examples but I get EXEC_BAD_INSTRUCTION thrown.
I have checked that the tabcontroller is correct before I replace by doing
println("number of navs: \(self.tabBarController?.viewControllers?.count)")
And that prints the correct number of controllers.
Any ideas?

Resources