IOS Remove CCLayer and Replace With ViewController - ios

I'm going to preface this question with the fact that I'm a complete novice in IOS, and I'm trying to learn as I go here.
The app I'm working on has a single Navigation Controller containing a Title Screen View Controller containing the app options. When I click the start button in the Title Screen it takes me to the Main Game Screen View Controller which has a CCLayer for doing some simple Cocos2D animation. All of this is working very well. Here's what I'm missing. I'm trying to programmatically go back to the title screen. I'm thinking there might be 2 options
1 - End the CCLayer somehow which will return me back to the Main Game View Controller where I can then send a command to go to the Title View Controller?
2 - Get a reference to the parent view controller of the CCLayer, and do the same as above.
I've scoured the internet for a couple of days trying to find what to do, and I'm out of ideas. I don't really have anything to show apart from I think I need to initialize the Title View Controller again.
Any ideas will be appreciated, thanks for your time.

well as you mentioned, you have a navigation controller which means that your Main Game View controller is pushed over your Title View Controller.
and your CCLayer is added on MainGameView Controller
so all you ever need to do is to pop your MainGameViewController. that shall automatically take you to your titleViewController.

You just need to write in your acton method
[[CCDirector sharedDirector] popScene];
You will be in previous scene.

Related

Does calling viewController without dismissing it create a second instance?

I have been searching all over the web but I can't seem to find the answer to this.
Currently i am using presentViewController to start new ViewControllers, but on certain view controllers i do not dismiss it and call over it. I currently am not using any navigation controllers or anything like that.
I am just worried that if I call the same viewController again via presentViewController, that the same viewController would have 2 running instances.
Is it possible? Or does the iOS framework automatically reuse the idle viewController?
If so, how do i remove the idle view controllers?
Thank you! (I was holding back my question and tried to find it all over the web, so if you can point me in the right direction, it would be very helpful thanks!)
iOS will not reuse your view controller, you can easily check it yourself by printing your view controller in viewDidLoad, you will notice first that viewDidLoad is called every time, and next that all objects have different addresses.
Unless you create thousand of them, or the navigation of your app doesn’t let you come back to an “idle” view controller, I would not say this is an issue though.
I don’t see any clean way to remove a view controller from the memory without calling “dismiss”. You could try to:
- “refresh” your view with new data.
- use something like UIPageViewController if the workflow of your app allows this kind of behaviour.
- rework the navigation so you can dismiss the view before calling another one
Good luck

Not able to get the alert with main viewcontroller

I have a One main view controller and in that I am having two sub view controllers named like rightViewcontroller and leftViewcontroller. When we press the menu items from the right view then I am changing the left view controller.That's working fine.Here my problem is If I get any alert that's only fixed to any of the View controller's.Then user able to press the other view which is not having the alert(those are custom alert's. I need custom alerts as per the client requirement)in iPad. Please Help me
Thank's in advance
When you get an alert on, say, the leftViewController, then disable the user interaction on the other viewControllers.
something like:
rightViewController.view.userInteractionEnabled = NO;
It seems when you present your alert you are showing it over one of the views like showInView:self.view when in fact you should show it over the parent view so it covers both like showInView:self.navigationController.parentViewController.view (that's just pseudo code).
Without seeing your code you use to present it currently I can't give you exact code.
Another way would be to first get a pointer to the parent view controller or navigation controller and show it there.

Push segue from a view controller to itself doesn't animate

I have a board game app that I'm programming which involves a player asking the same question to the other players in a clockwise direction. Since the question is the same for all players, after one player answers the question I want another instance of the same view controller (and its views) to be presented again (with some minor changes in the views to reflect the fact that a different player is being asked the same question).
So here's the problem: When I programmatically try to push the new view controller of type "PlayerAnswerToSuggestionVC" from the first instance of "PlayerAnswerToSuggestionVC" view controller with the following code, there is no animation:
PlayerAnswerToSuggestionVC *patsvc=[self.storyboard instantiateViewControllerWithIdentifier:#"PlayerAnswerVC"];
[self.navigationController pushViewController:patsvc animated:YES];
The new view controller does appear but there is no animation of the transition despite the fact that I am declaring that I do want animation in the "pushViewController:animated:" method. Same thing happens when I try to use a storyboard segue from a UIButton of the first view controller to instantiate a view controller of the same type. The new view controller appears, but without any push transition animation.
I realize that this may be an intended feature and that oftentimes one may not want an animation when going from one view controller to another of the same type. But in my case I do want a visual push transition in order to reflect the fact that the game question is now being directed at a different player. Any ideas on a simple way to implement an animated push transition from an instance of one view controller to another view controller of the same type?
*Additional info discovered: After playing around with the app some more, I have to amend some of my comments above. It appears that the push transition doesn't animate but later in the game the same push transition in another instance of the same view controller class DOES animate. I still have no idea why the push transition doesn't animate the first time time it is attempted, though. Still looking for a pattern to try to understand what is going on...

My iOS app complains about two-staged rotation but I'm not using it

I have to support down to iOS 4.3.
My app outputs in the console :
Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations.
As far as I know I'm not using two-staged rotation. I just have this method in my view controllers :
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
What else should I check in order to fix that?
Edit:
More precisions: my app uses a UITabbarController subclass. When the app starts, it checks if a user is logged in and then initiates the controllers of the tabbar controller if it's the case. If there's no user logged in, a modal view is presented (from the tabbar controller) the prompt the user to login and the controllers of the tabbar controller aren't initialized (yet). The "two-staged rotation" error is shown only at that moment and the rotation doesn't work.
So to summarize, the problem happens in that situation:
The rootViewController of the main window is the tabbar controller
The tabbar controller is empty (there are no view controller in the tabs and there's no tab)
A view controller is modally presented from the tabbar controller
OK I found a solution.
It seems like the presented modal view won't rotate until the viewControllers property of the UITabBarController is initialized. Since the concerned modal view is actually for login, I don't want to display anything behind it until the user is logged in because the views intended to be hold by the tabbar controller depend on the fact that a user is logged in.
So now just before presenting the modal view, I initialize the tabbar controller with a single, empty UIViewController and I remove it when the modal view is dismissed (i.e. a user logged in).
Perhaps it seems like a hack but it works well. And even if I don't understand why, it doesn't seem completely illogical that the tabbar controller doesn't behave like we want until it is fully initialized.
If someone has a better solution or explanation, feel free to comment :)
There aren't many posts regarding this error, so I'll admit my own shortcomings for the benefit of the next person so focused on the trees they might miss the forest. I found a missing
[super viewWillAppear:animated];
call inside my sub.

iOS - pushViewController vs presentModalViewController difference

What is the difference beetween calling presentModalViewController and pushViewController, when :
animation is set to NO (even if yes, that's just an animation style that can be changed).
a navigation controller is defined when presenting the modal view, so it can be navigable too, with a call stack, ....
Is this just to be able to go back from the first pushed view ? Woooaaaaaa.....
I guess the difference is elsewhere and deeper. No ?
Ignoring transitions/animations and how things are structured behind the scenes (which aleph_null's alswer provides a good discussion of), the only user-facing difference is the ability to return to the preceding view automatically using the navigation bar.
If you use pushViewController you will automatically get a "Back" button in the navigation bar. If you use presentModalViewController you do not, and generally will have to implement your own controls and/or callbacks to handle dismissing the controller.
Conceptually the modal presentation style is generally used for atomic tasks that you cannot navigate away from (i.e. you either complete the task, or you cancel, and you cannot do anything else within the app until you do one or the other).
If you're wondering why have the difference in the first place, I can't say. Personally I think frameworks that provide a unified API for moving from one controller to another (like cocos2d, or Android) make a lot more sense.
The most important difference is about semantics. Modal view controllers typically indicate that the user has to provide some information or do something. This link explains it more in depth: http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html
Here's another, less abstract difference they talk about:
"When you present a modal view controller, the system creates a parent-child relationship between the view controller that did the presenting and the view controller that was presented. Specifically, the view controller that did the presenting updates its modalViewController property to point to its presented (child) view controller. Similarly, the presented view controller updates its parentViewController property to point back to the view controller that presented it."
Also see this thread: why "present modal view controller"?
Take a look into the viewControllers in the image
The top 2 viewControllers(login & submit) at the top left are disconnected from the tabBarController & NavigationController
The rest of the viewControllers are embedded in a NavigationController. They somehow belong to the natural flow of the app.
Now you have to ask yourself
Do I need to always show login + submit page every time? It would be pain in the neck for the user to each time go to login even if they logged in last time. These 2 screen really don't fit the natural flow of the screens. So what do we do? We just add them modally using presentViewController
However for the rest of the viewControllers we want to keep them inside 2 navigation so we can easily go back and forth so we use pushViewController
For more information I recommend you to see this video
The image was also picked from this great answer. It's worthy of a look.
This is what my experience says,if you want to manage a hierarchy of views,better go for pushViewController in the navigation controller. It works like a stack of view-controllers in the navigation controller. If however the requirement is just to show a view on executing some actions on the parent view controller then the best way is presenting it modally.
If you need a complex push pop logic always prefer a pushViewController.
UINavigationController are used when you want to have some sort of hierarchal representation of your data (ie drill down). They work using a stack of UIViewController subclasses. Every time you “drill down”, you simply add another view controller to the stack. Then, the “back” logic is simply a matter of popping view controllers off of a stack.
You can check out this link:
http://www.icodeblog.com/2011/10/11/back-to-basics-an-introduction-to-view-controllers/

Resources