I'm using Google AdMob and to present ads, I do so from the ViewController. Right now, I have a button in my GameScene that is supposed to tell the ViewController to display the ads.
At first, I tried delegation, but that didn't work since SKScene already has a delegate, but then I stumbled upon this answer that showed a way for the SKScene to call a method in its ViewController. The answers seems sketchy, though, as the SKScene holds a reference to its ViewController. My understanding, as well as according to this, was that this is bad practice, assuming that in my case, the SKScene is the View in the MVC structure.
So how should I notify the ViewController of events occurring in the SKScene?
You can check my answer which describes how to communicate SKScene with UIViewController using delegate pattern.
Related
I'm looking for a solution to track everything a UIViewController gets instantiated or is displayed on screen without using a base view controller as a superclass.
I know Firebase Analytics do it for the event screen_view, but haven't found a similar way to do it yet.
Any ideas on how to achieve this?
I am working on a game with Sprite kit (swift) and I need a menu with buttons, stats, store etc.
I know I can make it with a SKScene but I want to edit it in the MainStoryBoard not only with code like in SKScene. If I create a new UIViewController file then just segue a button from that view to the SKScene will that be enough? Is it acceptable in terms of efficiency and memory management?
I think that will work fine for you, but it might be harder to work from a new UIViewController file than from SKScene. How effective creating the new UIViewController file versus creating the SKScene in code depends on how comfortable you feel in swift, so its hard to tell which you would prefer. I do not see any problems with memory management, but it could be less efficient than an SKScene because an SKScene was arguably designed for what you are trying to do. In summary, I believe it will be fine for you to use a new UIViewController, but an SKScene is likely the better route.
If you have a view controller in your storyboard (which you probably have if you go with apple's template), add a swift file like you do for any other app and connect it to your view controller.
Add any ui elements you want and add any actions and outlets.
Just make sure your SKScene is being shown IN your view controller. In most cases that is precoded.
SKScene can also be implemented in a UIView, so you can also drag a UIView in a view controller and set it as an SKScene file.
Hope it helps :)
As simple as this question may seem, I just couldn't figure it out. To the overflow community, I am using sprite kit to create a simple game, However when you lose the game I want to programmatically segue back to another viewController (basically the home screen). from The game scene I do not get access to "performSegueWithIdentifier" I've seen a couple answers that relate to this topic however non of them are in swift ... Any solutions?
links of related questions include : Link1
Thank You
I think you are misunderstanding SKViews. SKViews are just like a normal UIViews, they take place within a normal ViewController. You will therefore need to segue from the ViewController which is presenting your SKView.
I have the same issue in a game I have created (Ninja-Shooter). In my game, whenever I want to segue back the home screen I pass a value to NSUserDefaults.standardUserDefaults(). Then in my ViewController I am constantly listening for any change in that value, if the value does change, then I perform a segue.
Hope this helps!
I have discovered that using NSNotification Center Can in fact derive the results I was looking for. If any one in the future needs detailed code I will edit to provide that
I've got a stupid UI Problem right now with one of our apps.
I start with a UINavigationController which is the Initial View Controller in my Storyboard. The Root view controller is a UIViewController witch a UIScrollView as subview. The UIScrollView itself has paging enabled and contains several UIViewControllers.
Now here is my problem: The UIViewControllers (lets call them View #1) in the UIScrollView should react to a button event and make a segue to another UIViewController (View #2) which needs to get data from the previous UIViewController and needs to send data back by delegate methods.
When I try to get the UINavigationController and push View #2 on it, nothing happens. Showing it via presentViewController of course works.
Could you just help me a little bit, tell me that this doesn't work and when not why? I'm a little bit stuck at this point and i don't know how to proceed.
DO NOT put UIViewControllers inside another UIViewController (atleast pre iOs5) that goes against apples development guidelines ..
The UIViewControllers life cycle is managed internally and if you directly add the UIViewController as a subview a lot of methods such as
viewDidLoad
viewWillAppear
viewDidAppear
.
.
.
will not be called and from my experience will lead to really erratic behavior
Try and use custom UIView objects to get your internal logic for each view
agreed this somewhat breaks the MVC pattern but this would be the best way to go about your issue
But iOS 5.0 added containment UIViewControllers that correctly handles those lifecycle events by adding child view controllers. so if you are targeting only post iOs 5 devices this maybe a good way to about it
Have a look at this and this
Playing around in Instruments, I noticed something I don't understand. I create a couple of UIViewControllers and add their views to another UIView:
CustomVC *vc = [[CustomVC alloc] initWithCustomInitializer:someParameter];
[mainView addSubview:vc.view];
By logging the memory address of the CustomVC in its init and dealloc, I see that vc is deallocated almost immediately, though the view remains on screen and everything works fine.
Does the UIView not necessarily need its controller? Or is something else going on that I'm misinterpreting?
A UIView doesn't/shouldn't retain its parent (the UIViewController), so if you drop your reference to the UIViewController and only keep the reference to the UIView, nothing keeps a strong reference to it and it will be released.
The weak reference in the UIView to its controller will be automatically set to nil.
Whether the UIView needs its "lost" controller to do its work is another story though, and entirely depending on the UIView.
UIView does not need ViewController by any means. What is happening is that you are probably not retaining pointer to VC. Pointer to view is retained by mainView.
Furthermore regardless wether you are using ARC or not. If you want to keep object in memory you need to have a strong(ARC) or retain property to it. Not to confuse you. It doesn't necessarily need to be your custom subclass. Lets say you are using UINavigationController. You can create a controller in app delegate for instance and push it to UINavigation controller. You dont need to keep a pointer to it as long as something does. This is very fundamental. You need to be clear on memory management I dont think I will be able to explain it here adequately. I would highly recommend you to check Stanford course on iOS development.
http://itun.es/ru/_zEGD
There should be only one UIViewController at a time. Adding a view that belongs to a viewcontroller is bad practice.
To answer your question, because of the above mentioned constraint (or design decision by Apple), the view is probably now retained by the other view controller, and released from vc.