How can I choose which ViewController my App should load when the app goes back to foreground?
Is it by default the last view used in the App?
Is there a way to choose a different view or viewcontroller? If so
how?
Maybe you could use:
Apple Documentation-UIAppDelegate-applicationWillEnterForeground
to change your window rootViewController property :)
So basically, you could instantiate your view controller in this method(from storyboard, or all in code), and do the following:
self.window.rootViewController = yourVc
By default, your last view is the one shown by your application.
If you want to change it to another view (for example a lock screen view), you must change your window.rootViewController inside your ApplicationDelegate code, inside both
- (void)applicationWillResignActive:(UIApplication *)application
and
- (void)applicationDidEnterBackground:(UIApplication *)application
Related
I am working on an application and I want to implement a tutorial that would run the firs time the application is run on the Phone. I have already setup NSUserDefaults and can successfully determine and flag the application once it is run for the first time. My problem now is moving from the Tutorial View Controller to the main view controller.
My current set up has the main view controller set as the root controller to my app and my tutorial view controller needs to be shown and then dismissed only the first time the app is run.
What's the best method for implementing it? I was thinking of showing it as a Modal view and have the main view controller show it if it detects the first run. Any suggestions?
Note that I am not using Storyboards
Simply put, choose your root view controller programmatically. Something like:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([self isFirstLaunch])
{
self.window.rootViewController = [TutorialViewController new];
[self setFirstLaunch NO];
}
else
{
self.window.rootViewController = [MyRegularRootViewController new]
}
[self.window makeKeyAndVisible];
return YES;
}
I would go with your approach of presenting a modal on top of your root view controller when you detect first launch.
Use presentViewController:animated:completion: to display it, with animated: set to NO. That way it will appear on top of the root view controller and cover it. Then when you're done with the first view controller you can dismiss it and reveal your root view controller underneath.
I have a UINavigationController and on it I load a rootView that controlls the login process to my application.
I have recently added some code to my application delegate that checks my settings bundle for a logout request when this logout request happens I would like to either reload the rootView so that it loads the login hud, or just call the method inside rootView that shows the login hud.
This is how I set up the rootView for the navigationController inside my appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.navigationController; //Adds RootViewController to the NavigationController interface
// etc
}
What I would like to know is there a way to reload rootViewController? or call a method from it inside the application delegate?
It can be done but it's complicated. Best to avoid it if possible, and the specific requirements will be different for every app. Without seeing the source code for your app we can't tell you how it's done.
The basic process is you need to remove all of them from the view and set all references to nil, and then re-create it either from code or by loading the nib again.
A far better option is to leave the rootViewController where it is, and present a modal login view controller over the top of it. Once the user has logged in, send an NSNotification that the root view controller can observe, and populate its data.
Wait until after the notification has been sent to hide the login controller, and consider having the root view controller block the main thread while it performs any network operations pertaining to logging in. This way the login view (with a "logging in..." message?) will remain visible until the root view is fully populated.
I was surprised to observe that within didFinishLaunchingWithOptions a call to [UIApplication sharedApplication].keyWindow.rootViewController returns nil.
I want to set some things up once on app launch where I need to reference the rvc, however this behavior means I'll have to do it elsewhere.
If it can't be done in didFinishLaunchingWithOptions then the only other choice is applicationDidBecomeActive? But with the additional irritation (a small irritation, but still you'd think it shouldn't have to be necessary) of having to have a flag to ensure the set up steps only happens once and not every time appliationDidBecomeActive is called.
Is there somewhere else I can access the rootViewController on app launch to set additional steps up once?
There isn't a keyWindow at that time in the application lifecycle, so the reason there's no rootViewController to get on the keyWindow is because keyWindow is nil. But the app delegate has a property for your window, so you can just get self.window.rootViewController.
However, if you always have the same root view controller, you could probably do at least some of what you want to do (maybe all of it) in your root view controller's viewDidLoad method. This generally should only ever be called once, because with iOS 6 and later your offscreen view controllers' views are never unloaded.
You can get it from window:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *rootViewController = self.window.rootViewController;
return YES;
}
I'm new in iOS and I'm working with Storyboards.
I have an application with some views.
My rootViewController (1) is a UINavigationController that connects to other views. At one point in the application (2), I include a component (SWRevealViewController, Facebook Side Menu Style) that is an instance of UINavigationController, so I have two UINavigationControllers nested within each other. I want to remove or change the first UINavigationController by the new one (2), and just have only one. All views are accessed via custom segues.
Detailed Image Here
I think the solution is to change the rootViewController before loading the view (2), and set the second UINavigationController as the main of the application.
Detailed Image Here
I tried to solve it accessing by:
[UIApplication delegate].window.rootViewController = myController;
but I only have nil or a empty window.
I read many post that the solution could be in my AppDelegate in the method
- (void) applicationDidFinishLaunching: (UIApplication *) application I can't understand how to apply it to my structure, because that method is called when the application is launched.
I think that my workflow application is wrong.Any feedback or help is welcome!
Thanks in advance.
It's fine to change the root view controller from another controller, but your code is wrong. It should be:
[UIApplication sharedApplication].delegate.window.rootViewController = myController;
If you're doing this action from a controller whose view is currently on screen, you can do it this way instead:
self.view.window.rootViewController = myController;
This should work as long as myController has been properly instantiated.
You could possibly remove (1) or off load it into another view controller that is hidden and once the user goes back to a point where you want (1) back you can load it back in. This could be done in the - (void) applicationDidFinishLaunching: (UIApplication *) application.
In my app I want to set a UIView (called "functionView") ever visible in my app, and when I change view controller it should stay ever in its position.
I show you an example:
as you can see view function is ever in its position and it shouldn't change its position.
Can I do set this view from appDelegate? and how I can control it? Can I use a class (subclass of UIView)?
**IMPORTANT: it shouldn't be a tab bar but a view that with animation can move over view controller
thanks
You should use a container view controller, which contains the functionview, and a view controller for the first and second views
you will need to manage the navigation/transition logic inside this view controller
this question has a link to a good WWDC video and a blog post about this: Container View Controller Examples
You can add subview to the main window of the application instead of the root viewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//...
[window addSubview:functionView];
//...