What is the best practice to switch between multiple views; change the rootViewController or use modal views?
Setting the rootviewController:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc : UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
var window :UIWindow = UIApplication.sharedApplication().keyWindow!
window.rootViewController = vc;
window.makeKeyAndVisible()
Changing the modal view:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
self.presentViewController(initViewController,animated: false, nil)
I'm confused as to which to use when I need to present the user some other view.
p.s. In my case, I've an app starting with the login form as the rootViewController. After login, I think it's best to change the rootViewController but am I right?
My suggestion is instead of bothering lot about it you just got to override the rootviewController rest of the thing is taken care by your app.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initViewController: UIViewController = storyBoard.instantiateViewControllerWithIdentifier("viewTarget") as TargetViewController
self.window?.rootViewController? = initViewController
In this code we are just overriding rootview controller from AppDelegate.
The point of the "rootViewController" is to a a central pivot for all of your views.
See it as a the top-most link in series of chains. e.g. views on the left, can talk to views on the right can talk to views in the middle thanks to the "rootViewController". the "common link".. On top of that, it controls (or has the capability of controlling) how the hypothetical views on the left, right and middle are configured etc...
Fine, I worded it a bit trivially, but the (UINavigationController) is specifically for hierarchal content i.e. a UITabViewController is a great example..
I've just noticed a great post on here (Changing root view controller of a iOS Window) whilst finding a decent image to explain it decently.
Kudos to Matt on that post :) Hope this helps.
If not feel free to ask away :)
edit and for reference for others, here's something cheap and quick I slapped together that might get your head wrapped around storyboards :)
https://dl.dropboxusercontent.com/u/61211034/viewFun.zip
Cheers,
A
The application window's root view controller is the first view loaded into the window after the splash screen. Although it is possible to switch the window's view controller to change the view, consider using a UINavigationController and pushing additional view controller onto the view stack. This gives the user more flexibility, allowing the user to go back and forth between views.
Related
I have two view controllers which are defined in storyboard as,
UIViewController -> (Show Detail Segue) -> UITabBarController
My problem is; I am using a library called netfox to debug my HTTP requests. And this librarie's UI is being triggered by Shake Gesture. But when I come to the UITabBarController Shake Gesture on the simulator does not work at first time. At the second time it dismisses the ViewController that is currently on the screen and obviously a child of UITabBarController and goes back to the initial UIViewController. This is exactly like connecting two ViewControllers with modal segue and calling self.dismiss() from the child one.
I tried to change rootViewController of the UIApplication by,
UIApplication.shared.keyWindow.rootViewController = self
in the viewDidLoad() method of the UITabBarController and it worked. However, for this solution, the items(buttons, titles) in the UINavigationBar of any UINavigationController that is the child of UITabBarController are missing.
I have no idea why this is happening. If someone helps me while I am solving this, I would be really appreciated.
Instead of using
self.performSegue(withIdentifier:_)
calling this in the UIViewController have worked:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("tabBarcontroller") as UITabBarController
UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;
Respect to this answer.
I am new in swift.I want to move one screen to another but problem is when I go to another screen,old screen overrides in new screen.
here is my code
dispatch_async(dispatch_get_main_queue()) {
let appsDelegate = UIApplication.sharedApplication().delegate
appsDelegate?.window!!.rootViewController = nil
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("launcherEntry") as! UINavigationController
appsDelegate?.window!!.rootViewController = nextViewController
}
If you use UINavigationController, you should push the second view(screen) onto the first one. Check out the offical guide.
According to your code old screen overrides because you have not a push a UIViewController into navigation controller.
Here you destroyee the exiting controller then set a new controller. So that previos controller is move from the memory.
Please show Apple docs for the UINavigationController.
Update:
Please use following code for set a root view controller then you can push view controller easily.
let navigationController:UINavigationController = storyboard.instantiateInitialViewController() as! UINavigationController
let rootViewController:UIViewController = storyboard.instantiateViewControllerWithIdentifier("ID_LoginVC") as! LoginVC
navigationController.viewControllers = [rootViewController]
self.window?.rootViewController = navigationController
As you are new , i will explain what will happen when your code will be executed.
App delegate will hold the current instance of our application. Setting root view controller is setting Main view controller of your application.
SO basically your code will replace the previous view controller with next one.
Now what you have to do,
We have Navigation controller to manage our navigation stack, that allow us to go back from where have initiated.
If you are Using storyboard , add your view controller into NavigationController, if you already know then ignore or follow this
SELECT your first view controller GOTO Editor in Xcode menu > Select EMBED IN option > NAVIGATION CONTROLLER. this will add your first view into navigation controller.
Now when you want to display next view controller, There are two ways to do so,
1)Using segue from Storyboard
Right click from view/button from where you want to show next view.
drag it to the next view controller and release the right click.
Select push
2)Programatically
you don't have to set root view controller, just push the next view controller in navigation controller you have
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("launcherEntry") as! UINavigationController
Above line will intimate the textview controller, now we have to push this view controller, we can achieve this with
self.navigationController?.pushViewController(nextViewController, animated: true)
I have a side menu with different navigation points. The first point is the Home nav. This should send the user back to the main view.
If I start the app, the main view will be generated by the storyboard. Now if a user taps on "Home", I would like to reuse this instance. Is this possible? (With Swift)
Thanks
You can instantiate a view from a storyboard like so:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeViewController = storyboard.instantiateViewControllerWithIdentifier("Home")
Then do as you will :)
So what I am trying to do is have a separate story board for all of the buttons in my app. EX for each of the buttons below I want it to take it to another storyboard with more then one view controller where I can then do separate screen switching for each number in the range specified below and at the end have a quick quiz. But if I use a single storyboard all the screens would be to clustered to work with. Because in my main menu I have three lesson options (Numbers, General communication, Travel) each of those have 10 sub-lessons and each sub-lesson has 10 signs then a quiz at the end.
So in my xcode project I have the storyboards:
Main
0-9
10-19
etc...
How can I switch from the Main storyboard to 0-9's storyboard.
Why don't you create one generic view and show what you want there? You can send the data(which quiz you have selected) through prepareForSeque function and then in the view controller prepare data you want to show. I think that every quiz have something in common, or you can group them as a true/false, MCMA and MCSA. Just a suggestion.
Just read your comment, can you relate to statements above?
To answer your question:
UIStoryboard *story = [UIStoryboard
storyboardWithName:#"storyboard_name"
bundle:nil];
CustomViewController *controller= [story instantiateInitialViewController];
After this you can push controller to navigation stack or something like this:
[self.navigationController pushViewController:controller animated:YES];
Ofcourse, if you want to do this via IB you can extend UIStoryboardSegue and do similar logic there.
Since I am a bit blind, I pasted objetive c code, not swift code.
Here you go:
let storyboard = UIStoryboard(name: "myStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("myVCID") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
I'm working on an app which uses Facebook integration, and the log in system works fine now. However, I can't seem to return to my initial view controller when the user clicks log out.
Here's an overview of my storyboard:
I would like to return to the start when the user clicks the blue button (on the top). What would I do to achieve that? As you can see I have multiple Navigation Controllers, and I only use Push-seguesto get there. However, I do use the SWRevealViewController, which you can see in the middle.
I've tried [self.navigationController popToRootViewControllerAnimated:YES]; which doesn't do anything.
Any advice? Anyone familiar with the SWRevealViewController and what it might have done to my Navigation stack? Any help will be appreciated!
Thanks.
Try this,
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:#"NameOfYourStoryBoard"
bundle:nil];
LoginViewController *add =
[storyboard instantiateViewControllerWithIdentifier:#"viewControllerIdentifier"];
[self presentViewController:add
animated:YES
completion:nil];
Write Below Method in root viewcontroller
- (IBAction)returnToDashboard:(UIStoryboardSegue *)segue;
Give segue connection to destination view controller like below
Give identifier to segue and assign method to that segue
use below method in destination view controller
[self performSegueWithIdentifier:#"pushtoDashboard" sender:self];
First of all, I don't think you need that many UINavigationControllers. Using only one in your application should be enough.
The reason popToRootViewController is not working in your case is because it will go to the first view controller withing a UINavigationController. You have nested many UINavigationControllers, thus when you click the blue button in the settings view controller it will go to the sidebar view controller (can't read it properly, the image is small).
You can do the following to get to the root view controller of your app:
UINavigationController *rootController =[(UINavigationController*)[(AppDelegate*)
[[UIApplication sharedApplication]delegate] window] rootViewController]];
Replace AppDelegate with however it's called in your app.
But my advice is to remove all intermediate UINavigationControllers. Then just doing popToRootViewController should do the trick.
Problem
You'd like to return from a view controller (source) to the start (a
destination view controller) when the user clicks the blue button (on the
top)
Recommendation
I recommend you take a quick look at the highly rated SO answer which demonstrates how to use the unwind segue (that's the little green exit box on your view controller in the storyboard). Unwind segues are the modern way of accomplishing your goal, but you can also call dismissViewController:animated from the source controller. You should also take a quick read of a very small Apple note (TN2298) on using the unwind segue.
Essentially you will want to add the following method to your destination view controller:
- (IBAction)unwindToMainMenu:(UIStoryboardSegue*)sender
{
}
Then use ctrl+drag and click from the blue button down to the green exit icon on the source view controller. This will popup a menu and you can select unwindToMainMenu from the list. You will need to give the new segue an identifier in the Identity Inspector (e.g. segueToMain).
Manual Unwind
The technical note above (TN2298) will also show you how you can create a manual unwind segues that may be called programmatically (similar to how one might say performSegueWithIdentifier...).
I was working on a very similar problem. I am using a storyboard with a navigation controller & implemented the highly recommended SWRevealViewController, with iOS 7 & Xcode 5.1. I tried unsuccessfully to implement some of the solutions mentions. Either the screen didn't change or I got blank table. I used a hybrid version of the programatic examples provided in SWRevealController & other answers here to get a working solution. I added this as apart of my login button action
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
InboxTableViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:#"inbox"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
[self.revealViewController pushFrontViewController:nav animated:YES];
I retrieved my storyboard & initiated the view controller I wanted from the storyboard & then added to a navigation controller. Finally I used the SWRevealViewController method to Push the view I desired to the front.
I'm using swift and what worked for me was this:
var loginVC: UIViewController? = self.storyboard?.instantiateViewControllerWithIdentifier("UILogin") as? UIViewController
self.presentViewController(loginVC!, animated: true, completion: nil)
When you have different storyboards simply "presenting" the required VC from the initial storyboard does the trick:
Swift 3
if let loginVC = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController() {
present(loginVC, animated: true, completion: nil)
}
In some cases there might be leaking UITransitionView's. You might remove them right after the "presenting" code, but not before it and not in it's completion:
if let subviews = UIApplication.shared.keyWindow?.subviews,
let transitionViewClass = NSClassFromString("UITransitionView") {
for subview in subviews where subview.isKind(of: transitionViewClass) {
subview.removeFromSuperview()
}
}
<– This works as of Xcode 8.2.1 and iOS 10.2 but no guarantee if will work forever. Be careful with it.