Proper way to clean up views after calling presentViewController? - ios

I got an app with a logoff button on all of my pages. When the logoff button gets pressed i want my app to go to my login screen. I do this by calling the following in my IBAction for the logoff button:
loginScreen = [[GP_MobilViewController alloc] initWithNibName:#"GP_MobilViewController" bundle:nil];
[currentView presentViewController:loginScreen animated:YES completion:nil];
Now my problem is i don't know how to remove all of the previous created screens. Because form what i can tell, presentViewController won't remove anything for me, so i have to do this cleanup myself?
PS. I'm not using a UINavigationController. So popToRootViewControllerAnimated:YES, Won't work for me. I need another solution. Thanks in advance.

self.view.window.rootViewController = self;
or in the completion block,
loginScreen = [[GP_MobilViewController alloc] initWithNibName:#"GP_MobilViewController" bundle:nil];
[currentView presentViewController:loginScreen animated:YES completion:^{
[UIApplication sharedApplication].keyWindow.rootViewController =loginScreen;
}];
or in your loginViewControllers-viewDidAppear also you can set it as rootViewController to window.
This makes all the viewControllers to be freed.

Related

Returning back to the same UIView makes it blank

I'm completely new to Objective-C and XCode. In my game, I have a separate UIView which shows up when the user loses the game. There, I have a Try Again button which reloads the previous UIView which runs the game. However, when I go back by pressing, everything looks black and my images edges keep flickering and I cannot see anything. I'm just trying to be descriptive here so that you experienced folks can guess what is happening. Here is the code to pull up the Game Over View.
GameOver *gameover = [[GameOver alloc] initWithNibName:nil bundle:nil];
[self presentViewController:gameover animated:YES completion:nil];
And this is the code that is invoked when the user clicks the Try again button.
-(IBAction)tryAgain:(id)sender{
GameScene *gameScene = [[GameScene alloc] initWithNibName:nil bundle:nil];
[self presentViewController:gameScene animated:NO completion:nil];
}
Instead of the second part just use the line:
[self dismissViewControllerAnimated:NO completion:nil];
You're creating a new instance of GameScene and will not be the same as the first one.

how do I pop a view on a UIPageViewController?

I have a screen tutorial for my app. I have a UIPageViewController setup to manage 3 View Controllers. Once you get to the last view on the tutorial you press a "done" button and the tutorial is supposed to go away.
I can't seem to find a way to pop/dismiss the tutorial. I have tried the following:
[self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
Any ideas would be appreciated.
The View Controllers being managed are subviews of UIViewController if that's of any help.
EDIT
This is how I set up the views:
In my UIPageViewController I have the following:
-(void)viewDidLoad{
[self.pageController setViewControllers:#[tutorialPages[0]]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];
}
- (void)setupContentViews{
ScreenTutorial_1ViewController *screenTutorial1 = [[ScreenTutorial_1ViewController alloc] initWithNibName:#"ScreenTutorial_1ViewController" bundle:nil];
ScreenTutorial_2ViewController *screenTutorial2 = [[ScreenTutorial_2ViewController alloc] initWithNibName:#"ScreenTutorial_2ViewController" bundle:nil];
ScreenTutorial_3ViewController *screenTutorial3 = [[ScreenTutorial_3ViewController alloc] initWithNibName:#"ScreenTutorial_3ViewController" bundle:nil];
tutorialPages = #[screenTutorial1, screenTutorial2, screenTutorial3];
NSLog(#"tutorPages = %#", tutorialPages);
}
First, I strongly recommend you using:
[[NSUserDefaults standardUserDefaults] setObject:#YES forKey:#"hasSeenTutorial"];
Secondly, how are you presenting your PageViewController? In order to dismiss it like you have, you need to use (from a UIViewController):
[self presentViewController:tutorial animated:YES completion:nil];
Where tutorial is your Tutorial View controller. This will slide it up across the screen (or you can not animate it), and then you can dismiss it like you have.
Wild shot in the dark here, but I think you could reach your UIPageVieWController by calling self.parentViewController in your child controllers.
You could then either dismiss it (if it's modally presented) or pop it.
I just realized that I had 2 views in a xib file and the IBAction was connected to the wrong one. I've been copying and pasting buttons, labels...etc. On one of those pastes, I must have copied the pasted the entire view. I deleted it and used the following command which did dismiss the View:
[self dismissViewControllerAnimated:YES completion:nil];

dismissViewControllerAnimated prevents new UIViewController from showing

I am trying to replace one UIViewController with another, However I have ecountered a problem.
If I write this
[self dismissViewControllerAnimated:NO completion:nil];
//load currentProjectListViewController
currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
[self presentViewController:currentProjectListViewController animated:NO completion:nil];
This almost works, however the view just blinks and nothing happens.. no new view is loaded or anything, I have put a break point inside currentProjectListViewController and the thread never makes it there.
however if I do this.
//load currentProjectListViewController
currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
[self presentViewController:currentProjectListViewController animated:NO completion:nil];
currentProjectListViewController loads perfectly fine. however I am worried about whats hapening with the previous view? is it stuck there in memory? or is it gone?
my question is how can I dismiss it from memory as well as site without stopping my next view from appearing.
any help would be greatly appreciated.
No, the second way is the correct way as far as I see it. In the first method you're asking the VC to dismiss before a new VC is presented. This would be there is no view, which wouldn't happen.
Bu presenting a new VC, the old VC won't be retained in memory as the nature of the view is that it only uses memory when it is in view. I hope this make makes sense.
Try presenting the new view controller in the completion handler of the dismiss method call:
typeof(self) __weak weakSelf = self; //Need to have a weak reference to self to prevent retain cycle.
[self dismissViewControllerAnimated:NO completion:^{
currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:#"CurrentProjectListViewController" bundle:nil];
[weakSelf presentViewController:currentProjectListViewController animated:NO completion:nil];
}];

iOS 6 NavigationController not loading

I have an app that I migrated over from iOS 5 to iOS 6 and am having trouble with the UINavigationController not displaying correctly.
In my app, the user must login and they are presented that login screen in a modal view after pressing a button.
WelcomeViewController.m
- (IBAction)signInButtonSelected:(id)sender
{
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController presentViewController:loginViewController animated:YES completion:^{}];
}
The view loads correctly and has lets the user enter their login credentials. However, when they press the login button, the next view is presented and it SHOULD have a UINavigationBar at the top, except it doesn't. If I quit the app and restart it, the UINavigationBar shows up in that view properly. I have not a clue as to why going from the login view to the logged in screen hides the navigation controller.
Here is the code that is executed when the login button is pressed that loads the next view:
LoginViewController.m
WallViewController *wallViewController = [[WallViewController alloc] initWithNibName:nil bundle:nil];
[(UINavigationController *)self.presentingViewController pushViewController:wallViewController animated:YES];
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
If anyone can shed some light on to why this is happening that would be great!
Thanks!
Figured it out. In my App Delegate, I had placed:
navController.navigationBar.hidden = YES;
And it would hide the navigation bar after a user would login. Not exactly sure why this in the app delegate would affect a view later on down the line. But once it was removed, it works perfectly!
Thanks for the help!

How do I set UIButton to go to the previous screen?

I have a help screen that can be accessed from any other screen. The only button there is a "back" arrow. Right now it always goes back to the main screen. However, I need it to go back to which ever screen that was before it. I've hunted through stackoverflow and google but no luck.
EDIT
snippet of the code I'm using now:
appViewController *homeView = [[appViewController alloc] initWithNibName:nil bundle:nil];
homeView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:homeView animated:YES];
From the modal view controller that is being presented right now, make it dismiss itself when clicking the home button:
- (void)buttonClicked
{
[self dismissModalViewControllerAnimated:YES];
}
On button click You can use.
[self.navigationController popViewControllerAnimated:YES];
Maybe this link will help you: How to navigate previous screen from camera view in iphone?
Its about navigation and how to remove the last screen.
Or use this in the click event of button.Create an instance of the view controller to be opened
EnterNameController *newEnterNameController = [[EnterNameController alloc] initWithNibName:#"EnterName" bundle:[NSBundle mainBundle]];
[[self navigationController] pushViewController:newEnterNameController animated:YES];

Resources