I've created a screen that ONLY appears the first time that my app is launched; it works great. However, I'd like to present this screen modally on top of my root view controller (EditorsNoteViewController being the screen I want to present modally on first launch). Does anyone know how I would do this? Here is what I have so far:
Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
NSLog(#"not first launch");
self.viewController = [[UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"articlesNav"];
self.window.rootViewController = self.viewController;
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.viewController = [[UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:#"articlesNav"];
self.window.rootViewController = self.viewController;
EditorsNoteViewController *vc = [[EditorsNoteViewController alloc]init];
[self.viewController.navigationController presentViewController:vc animated:YES completion:nil];
}
[self.window makeKeyAndVisible];
return YES;
}
I figured out that you'll have to call
[self.window makeKeyAndVisible];
before presenting the view controller
I have read about and encountered this issue many times. A way to completely get around this issue (without any flickering or warnings) is to consider a custom container view controller that will contain your root view controller and your modally presented view controller, as child view controllers. The container view controller can then transition between the two with custom animations, with a very similar effect as present/dismiss.
Let's consider the following container view controller :
#implementation ContainerViewController {
NSMutableArray* stack;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
stack = [NSMutableArray new];
}
return self;
}
- (void) pushViewController:(UIViewController*) viewController {
UIViewController* currentViewController = [stack lastObject];
[stack addObject:viewController];
if (self.isViewLoaded) {
[self addChildViewController:viewController];
[currentViewController willMoveToParentViewController:nil];
viewController.view.frame = CGRectOffset(self.view.bounds, 0, self.view.bounds.size.height);
[self transitionFromViewController:currentViewController toViewController:viewController duration:0.3 options:kNilOptions animations:^{
viewController.view.frame = self.view.bounds;
} completion:^(BOOL finished) {
[viewController didMoveToParentViewController:self];
[currentViewController removeFromParentViewController];
}];
}
}
- (void) popViewController {
UIViewController* currentViewController = [stack lastObject];
[stack removeLastObject];
if (self.isViewLoaded) {
UIViewController* viewController = [stack lastObject];
[self addChildViewController:viewController];
[currentViewController willMoveToParentViewController:nil];
[self transitionFromViewController:currentViewController toViewController:viewController duration:0.3 options:kNilOptions animations:^{
[self.view sendSubviewToBack:viewController.view];
currentViewController.view.frame = CGRectOffset(self.view.bounds, 0, self.view.bounds.size.height);
} completion:^(BOOL finished) {
[viewController didMoveToParentViewController:self];
[currentViewController removeFromParentViewController];
}];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController* viewController = [stack lastObject];
[self addChildViewController:viewController];
[self.view addSubview:viewController.view];
[viewController didMoveToParentViewController:self];
}
#end
In your AppDelegate, you can then have something along those lines :
ContainerViewController* viewController = [ContainerViewController new];
[viewController pushViewController: [RootViewController new];
if ([self shouldPresentModalViewController]) {
[viewController pushViewController: [ModalViewController new];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Somewhere in the modal view controller, you can use the following code to dismiss it:
[((ContainerViewController*) self.parentViewController) popViewController];
Assuming that you have created your EditorsNoteViewController in a storyboard, you should initialise it like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EditorsNoteViewController *vc = (EditorsNoteViewController*)[storyboard instantiateViewControllerWithIdentifier:#"EditorsNoteOrWhateverIdentifierYouChose"];
Try to check your NSUserDefaults in your RootViewController -viewDidLoad, and if it is the first time, present the new UIViewController there (So that you don't need to mess around with your AppDelegate).
If you want to present it animated, you can do it in -viewDidAppear instead of -viewDidLoad.
Related
I am currently loading another viewcontroller on this code
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NewViewController *controller = [[NewController alloc]init];
controller.licensed =#"NO";
controller.delegate = self;
self.window.rootViewController = controller;
self.window.backgroundColor = [UIColor clearColor];
[self.window makeKeyAndVisible];
But the problem is when the NewViewController loads. The subviews of the previous view controller is in the background of the NewViewController
[self.rectHolder performSelectorOnMainThread:#selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
[self.btnSignup performSelectorOnMainThread:#selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
[self.view removeFromSuperview];
I tried to use this, but the subviews is still in the background of the new view controller
Edit:
I also tried to use this
NewViewController* viewController = [[NewViewController alloc] init];
viewController.licensed = #"NO";
[self presentViewController:viewController animated:YES completion:nil];
But the subViews of my previous viewcontroller is still in the background
In my application i am adding the child view controller using the following code.
self.onlineUserList = [[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"OnlineUserList"];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:self.onlineUserList];
self.onlineUserList.delegate = self;
navController.navigationBar.hidden = YES;
CGRect aRect = [[UIScreen mainScreen] bounds];
[navController.view setFrame:(CGRect){0, 0, aRect.size.width, aRect.size.height-47}];
[self addChildViewController:navController];
[self.view addSubview:navController.view];
[self didMoveToParentViewController:navController];
And i am removing the child view controller using following code.
[_onlineUserList removeFromParentViewController];
[_onlineUserList.view removeFromSuperview];
[_onlineUserList didMoveToParentViewController:nil];
It is working well. But after removing the child view controller then i am not able to do any action on the parent view controller. I think i am missing some thing navigational flow. Please help me.
Just get all Viewcontrollers in array , check this .
If you want all view controller of navigation ,
NSArray *currentControllers = self.navigationController.viewControllers;
if you want first pushedviewcontroller ,
UIViewController *firstcontroller = self.navigationController.viewControllers.firstObject;
if you want last object ,
UIViewController *Lastcontroller = self.navigationController.viewControllers.lastObject;
Now trace with this code , which viewcontroller you are removing .
or Another way is just give identifier of your parentviewcontroller in didfinishlaunchwithoption , like ,
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
SlideMenu = [storyboard instantiateViewControllerWithIdentifier:#"SlideMenu"];
then add method for topviewcontroller,
- (UIViewController *)topViewController{
return [self topViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController *)topViewController:(UIViewController *)rootViewController
{
if (rootViewController.presentedViewController == nil) {
return rootViewController;
}
if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
return [self topViewController:lastViewController];
}
UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;
return [self topViewController:presentedViewController];
}
now create one more method in your appdelegate to present your parentviewcontroller like ,
- (void)ShowMenu{
[SlideMenu ShowToViewController:[self topViewController]];
}
and then when you remove childviewcontroller then call ,
[[AppDelegate mainDelegate] ShowMenu];
I hope this info enough to solve your issue.
Currently, I have set up a log-in view for users of my app. Below is the code that presents this log-in view to the user:
// Handle how we present the view
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[NotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
WebViewController *wvc = [[WebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
} else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
What I'm attempting to do now is display a web-view first if the user has just updated the app. Below is what this new code looks like:
// Handle how we present the view.
if (self.notificationToProcess != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWNotificationsViewController *viewController = [[SWNotificationsViewController alloc] init];
viewController.initialDataID = self.notificationToProcess[#"Data"];
self.notificationToProcess = nil;
[self.navigationController pushViewController:viewController animated:YES];
}];
} else if (self.detailURL != nil) {
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *wvc = [[SWWebViewController alloc] init];
wvc.URL = self.detailURL;
wvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:wvc animated:YES completion:nil];
self.detailURL = nil;
}];
else if (![versionOfLastRun isEqual:currentVersion])
{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSString *url=#"http://someurl";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else if (versionOfLastRun == nil){
// First start after installing the app
}
else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
However, when I run the app, the web view is not displayed, I get a warning of:
Warning: Attempt to present ViewController on NavigationController whose view is not in the window hierarchy!
Can anyone help me diagnose the problem? Thank you!
Seems like your viewController is not in Navigation controller hierarchy. This means that your controller is not connected with navigation controller stack which should handle presented controllers.
The other thing that should help is setting your root view controller as follows:
self.window.rootViewController = self.ViewController;
EDIT: since you mention that you don't use storyboards, setting rootViewController should do the trick.
Try to set the window's rootViewController to the navigation controller after you initialize the navigation controller:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = navController;
This is how I'm doing it without storyboards.
The problem was that I didn't call self.navigationController dismissViewControllerAnimated:YES completion:^
After correcting this (see listing below), my WebView was shown as expected.
else if (![versionOfLastRun isEqual:currentVersion])
{
[self.navigationController dismissViewControllerAnimated:YES completion:^{
SWWebViewController *webViewController = [[SWWebViewController alloc] init];
NSLog(#"%#", self.window.rootViewController);
NSString *url=#"http://devstatwatch.drbsystems.com/releases_mobile.php";
webViewController.URL = url;
webViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self.navigationController presentViewController:webViewController animated:YES completion:nil];
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"VersionOfLastRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
}
So in my App.Delegate I'm doing this -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginView" bundle:[NSBundle mainBundle]];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController: self.loginViewController];
self.window.rootViewController = navigation;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
and in my login controller I'm doing this -
- (IBAction)login:(UIButton *)sender {
NSString *username = self.userName.text;
NSString *password = self.password.text;
[AccountUtils emailLogin:username password:password useCookie:true callback:^(NSDictionary *loginResponseJSON){
if([loginResponseJSON count] != 0){
[self performSelectorOnMainThread:#selector(displaySearchController) withObject:nil waitUntilDone:YES];
// [self performSelectorOnMainThread:#selector(switchState) withObject:nil waitUntilDone:YES];
} else {
//incorrect entry info view here.
}
}];
}
- (void) displaySearchController {
SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle:[NSBundle mainBundle]];
UINavigationController *navigator = self.navigationController;
[navigator popViewControllerAnimated: YES];
[navigator pushViewController: searchViewController animated:YES];
}
If I correctly login, I go to the second controller's view, but at the top I'm still allowed to go 'back' to the login page. I don't want that to happen and I thought this case would be taken care off by the popViewControllerAnimated line. How do I make it so that when I login, I am not allowed to go back to the login page?(in other words, I guess pop the login controller off the navigation controller's stack?)
If you want just to remove the loginVC you could set the new navigationController as the rootViewController of the AppDelegate after the user has logged in. So you could move the displaySearchController method in the AppDelegate and call this method (from the loginVC) after the user has logged in:
-(void)displaySearchController{
SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle:[NSBundle mainBundle]];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController: searchViewController];
self.window.rootViewController=navigation;
}
Otherwise, if you really want the push animation, you can remove the loginVC from the navigationController viewControllers stack in the viewDidAppear of SearchDispalyController:
-(void)viewDidAppear:(BOOL)animated{
NSMutableArray *stackVCs=[self.navigationController.viewControllers mutableCopy];
int idx=[stackVCs indexOfObject:self];
//this remove the previous viewcontroller from the stack
[stackVCs removeObjectAtIndex:idx-1];
self.navigationController.viewControllers=stackVCs;
[super viewDidAppear:animated];
}
Also in the loginVC just before push the searchVC you should call this to hide the back button:
[navigation.navigationItem setHidesBackButton:YES];
You don't want to use a UINavigationController with the LoginViewController and you don't want to push the SearchViewController. Instead, use a UINavigationController with SearchViewController and when you display it, make it the rootViewController.
Do this to achieve what you want.
Move the displaySearchController method to your AppDelegate.m file
Do declare the displaySearchController method in AppDelegate.h file
Now Define the displaySearchController method in AppDelegate.m file as :
- (void) displaySearchController {
SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:#"SearchView" bundle:[NSBundle mainBundle]];
UINavigationController *navigator = [[UINavigationController alloc]initWithRootViewController:searchViewController];
self.window.rootViewController = navigator;
}
Call a new local method showNewViewController from your loginController as :
[self performSelectorOnMainThread:#selector(showNewViewController) withObject:nil waitUntilDone:YES];
Now define showNewViewController in your loginController.m file as
-(void)showNewViewController {
AppDelegate *appDele = [UIApplication sharedApplication].delegate;
[appDele displaySearchController];
}
Don't forget to import the AppDelegate.h file to your loginController.m
This will certainly help you.
I Show a view using presentModalViewController. and from this UIView I want to push a UIView using UINavigationController.
I tried below code for this
[self.parentViewController.navigationController
pushViewController:objViewFullScreen
animated:YES];
But it did not works for me. so please can any one suggest how I push a view from ModelViewController.
Thanks
First you have to present your modal view controller inside a navigation controller:
MyViewController *vc = [[MyViewController alloc] initWithNibName:#"MyNib" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self presentModalViewController:nc animated:YES];
[vc release];
[nc release];
Then inside MyViewController you can do:
OtherViewController *vc = [[OtherViewController alloc] initWithNibName:#"MyOtherNib" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
-(void)pushViewControllerWithCustomAnimation:(UIViewController *)newViewController {
newViewController.view.alpha = 0.0f;
[self.view addSubview:newViewController.view];
[UIView animateWithDuration:1
animations:^{
newViewController.view.alpha = 1;
}
completion:^(BOOL fin){
if (fin) {
// finally display the new viewcontroller for real
[self.navigationController pushViewController:newViewController animated:NO];
}
}];
}