show another view controller but save state of current view controller - ios

I'm trying to create a small game where the user moves over an object and displays another view controller to display information. Upon pressing a button on the recently presented view controller, the view gets dismissed and shows the view controller I started off with. I've done this like this:
ViewController.m
OtherViewController *other = [self.storyboard instantiateViewControllerWithIdentifier:#"other"];
[self presentViewController:other animated:YES completion:nil];
OtherViewController.m
[self dismissViewControllerAnimated:YES completion:nil];
but when i dismiss it, the user starts from the beginning again. is there a way to save where the user is and continue from there?

You're presenting the new view controller as a modal. When you dismiss it, the previous view controller should be uncovered in it's previous state.
If it's not, then you need to look at the logic of your ViewController class.
You want your one time setup code in your viewDidLoad method. If you have code in your viewWillAppear:animated method, or your viewDidAppear:animated method that resets the state of your view controller then that is the problem.

Related

KVO When View Dismissed

I currently have a main web view (UIViewController) that presents a UIViewController like this:
[self presentViewController:controller animated:YES completion:nil];
I am trying to observe when the presented view controller gets dismissed using:
[self dismissViewControllerAnimated:YES completion:nil];
I have tried observing a variable in the main web view that the presented view sets when it's being dismissed, but that doesn't seem to work.
How would I go about observing when the presented view gets dismissed?
Any help would be appreciated.
Thanks.
Can you put some code in the viewWillDisappear method? You're not clear on why you actually want to observe the variable. However this method will be called just before the view moves off. Conversely you can run code in the viewDidDisappear which is run after the view pops.

Transfering control from 3rd view controller to 1st view controller and invoke a method

Basically i have an application which has 3 screen. 1st screen is a normal UIViewController and 2nd view controller is showing a table view controller and 3rd view controller is a normal UIViewController.
Now user moves from 1st--->2nd-->3rd(Not by Navigation but by present modal view controller).
For ex.
//Moving from 1st to 2nd view controller by Creating an object of 2nd View controller
//SecondViewController *secondViewController=[[SecondViewController alloc]init......]
//[self presentViewController:secondViewController animated:NO completion:nil];
//Moving from 2nd to 3rd view controller by Creating an object of 3rd View controller
//ThirdViewController *thirdViewController=[[ThirdViewController alloc]init......]
//[self presentViewController:thirdViewController animated:NO completion:nil];
But the problem now i am facing is, if some event happens in 3rd view controller i want to invoke a delegate method defined in 1st view controller.But i am not sure how that can be done?
Use NSNotifacation and pass whatever data you want to pass in notification object.
OR
Use #protocol
you can also use the custom delegate for that
here is an example for that http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-custom-delegates/ or http://www.iossdktutorials.com/ios-sdk-custom-delegates/

Refreshing a UIViewController from a Modal Controller

I have a view controller which displays a carousel control (iCarousel). The view is rendered correctly and the carousel is displayed. Right after that a modal is displayed which allows the user to agree to certain terms. I want that once they agree I refresh the viewcontroller which contains the carousel control. Basically, I want to rotate the carousel to some random index.
- (IBAction)accept:(id)sender
{
NewsViewController *newsViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"NewsStoryboard"];
[newsViewController loadNews];
[newsViewController.view setNeedsDisplay];
[self dismissViewControllerAnimated:YES completion:nil];
}
The above code does call the loadNews and fetches it but the view is never refreshed.
What happens to the carousel should really be up to the view controller that manages it, not the modal view controller. Make the modal controller do its thing and return whatever data it collects to its parent. The parent (in this case, the carousel's controller) can then look at that data and decide what it needs to do next (refresh, for example).
The problem is this line:
NewsViewController *newsViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"NewsStoryboard"];
That is not the old view controller; it is a new, unused copy of that view controller. You need to create a line of communication from the modal view controller back to the existing view controller.
The typical way to do this is through a delegate, which you set when creating the modal view controller. If you look at the Xcode Utility template, you will see that it illustrates this architecture. The original view controller sets itself as the modal view controller's delegate, and the modal view controller is thus able to talk back to the original view controller as it is dismissed.
This is such an important thing to be able to do that I talk about it at length in my book:
http://www.apeth.com/iOSBook/ch19.html#_presented_view_controller

Objective-C: How to load a different view on button tap?

I have an iPad application that has a button on one view. When I press the button I want it to load a second view. The second view I am trying to load is a CollectionView. I am not using, and do not want to use a UINavigationController.
Does anyone know how to load a second view on a button tap? Also, I will want to create a Back button that will go back to the previous view. The previous view could be different each time the button is tapped.
There is a decent amount of material online about this topic, but I can't find anything that will work or anything that is recent.
Here is the code I have now:
-(void)showCollectionView:(id)sender
{
NSLog(#"In ShowCollectionView");
ZHCollectionViewController *cvc = [[ZHCollectionViewController alloc]
initWithNibName:#"ZHCollectionViewController"
bundle:[NSBundle mainBundle]];
[self.view addSubview:cvc.view];
NSLog(#"After all the stuff");
}
When this runs both NSLog's are executed and the message shows up in the console, but nothing happens to the view.
Yo can try to present it modally:
[self presentViewController:cvc animated:YES completion:^{
}];
Before this call you can customize appearance of your 'cvc' by defining transition and presentation styles, for example:
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
To hide it call in ZHCollectionViewController, inside some button action I believe
[self dismissViewControllerAnimated:NO completion:^{
}];
There are several ways to do this, and the way you're trying isn't one of them. If you just want to add a view, not a view controller, you should have a xib file that is a view, not a view controller. You would have to make the controller whose view you're adding this collection view into, the files owner of this collection view, so you can hook up any outlets to it.
It's not correct to add another view controller's view to your view, unless you're making that controller a child controller. If you want ZHCollectionViewController to be the controller of the collection view, then you should add that controller as a child view controller. You can check out Apple's documentation on custom container controllers to see how that's done.
You didn't really say in your question, how this collection view is to appear. Do you want it to take up the whole screen, or do you want it to be a subview? If you want it to take up the whole screen, then it would be better to just change the window's root view controller to ZHCollectionViewController, or present it modally over the current view.

ViewDidLoad Method is called again

I have two View Controller. In the first I do some stuff and then I can push the second View Controller by clicking a button (the button connected with the other ViewController in the storyboard). There I can do some settings and so on. I get back to the first View Controller with the button "Done". But then the ViewDidLoad method is called again and all the "stuff" (text in textfields, ...) is gone.
I hope you guys understand my problem.
Why? And how can I disable this?
How are you going back to the first view controller from the second one? I think your problem is you're re-instantiating the first view controller when the user hits "Done".
Instead, you should be using either "popViewControllerAnimated" or "dismissViewControllerAnimated" to go back to the first view controller.
e.g: (one of these 2 should work):
[self.navigationController popViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
Maybe the firstViewController is unLoaded because of receiving memory warning. So when it opened again, it calls ViewDidLoad

Resources