I am pausing my game with a willResignActive notification, and it seems to pause the game, but when didBecomeActive is called, it seems to un-pause on its own.
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationWillResign)
name:UIApplicationWillResignActiveNotification
object:NULL];
- (void) applicationWillResign {
self.scene.view.paused = TRUE;
NSLog(#"About to lose focus");
}
How do I get it to stay paused? Do I actually need to pause it in my AppDelegate?
Here's a way to keep the view paused after returning from background mode. It's a bit of a hack, but it does work.
1) Define an SKView subclass with a boolean named stayPaused...
#interface MyView : SKView
#property BOOL stayPaused;
#end
#implementation MyView
// Override the paused setter to conditionally un-pause the view
- (void) setPaused:(BOOL)paused
{
if (!_stayPaused || paused) {
// Call the superclass's paused setter
[super setPaused:paused];
}
_stayPaused = NO;
}
- (void) setStayPaused
{
_stayPaused = YES;
}
#end
2) In the storyboard, change the class of the view to MyView
3) In the view controller, define the view as MyView
4) Add a notifier to set the stayPaused flag
#implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Define view using the subclass
MyView * skView = (MyView *)self.view;
// Add an observer for a method that sets the stay pause flag when notified
[[NSNotificationCenter defaultCenter] addObserver:skView selector:#selector(setStayPaused)
name:#"stayPausedNotification" object:nil];
...
5) In AppDelegate.m, post a notification to set the stay paused flag when the app becomes active
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[NSNotificationCenter defaultCenter] postNotificationName:#"stayPausedNotification" object:nil];
}
Related
Sorry for the basic Question, I'm fairly new to programming and trying to understand something in the code apple suggester for a certain solution to something I wanted to preform.
I created a simple notes app, very basic, currently I have:
1. CreateNote view controller
2. NotesList table view controller
So I wanted to add a behaviour when a note is being created and a user types below the keyboard so the text resized so the user can still see what he types and the text is not going behind the keyboard.
So I add some lines of code suggested by apple to accomplish that.
In the viewWillAppear called a method on NSNotificationCenter and I could not understand where is an NSNotificationCenter object is declared...?
So this is my CreateNote view controller(Please help me understand why they could preform this call):
#import "NMCreateNotesViewController.h"
#interface NMCreateNotesViewController () <UITextViewDelegate>
#property (weak, nonatomic) IBOutlet UIBarButtonItem *createButton;
#property (weak, nonatomic) IBOutlet UITextView *textField;
#end
#implementation NMCreateNotesViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// listen for keyboard hide/show notifications so we can properly adjust the table's height
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark - Notifications
- (void)adjustViewForKeyboardReveal:(BOOL)showKeyboard notificationInfo:(NSDictionary *)notificationInfo
{
// the keyboard is showing so resize the table's height
CGRect keyboardRect = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration =
[[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.textField.frame;
// the keyboard rect's width and height are reversed in landscape
NSInteger adjustDelta = UIInterfaceOrientationIsPortrait(self.interfaceOrientation) ? CGRectGetHeight(keyboardRect) : CGRectGetWidth(keyboardRect);
if (showKeyboard)
frame.size.height -= adjustDelta;
else
frame.size.height += adjustDelta;
[UIView beginAnimations:#"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.textField.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
[self adjustViewForKeyboardReveal:YES notificationInfo:[aNotification userInfo]];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
[self adjustViewForKeyboardReveal:NO notificationInfo:[aNotification userInfo]];
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.createButton) return;
if (self.textField.text.length > 0) {
self.note = [[NMNote alloc] init];
self.note.content = self.textField.text;
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Is NSNotificationCenter is part of UITextView?
No it is not. NSNotificationCenter is - as it's name says - a notification center. Objects can subscribe to notifications and post notifications with NSNotificationCenter to handle and notify of certain events.
They are using NSNotificationCenter to have the viewcontroller subscribe to UIKeyboardWillHideNotification and UIKeyboardWillShowNotification events.
Take a look at this one:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
NSNotificationCenter is designed to be used as a singleton ( I believe this is the correct term, correct me if I'm wrong ) so we access the NSNotificationCenter for this app's process by calling the class method defaultCenter. it adds observer 'self' ( which in this case is an instance of the view controller) and basically instructs it to send the message keyboardWillShow to the observer when a Notification under the name of UIKeyboardWillShowNotification is fired.
What object fires the UIKeyboardWillShowNotification? Well it's not a UITextView, this notification name is actually defined in UIWindow.h so it probably came from there, which in turn probably was invoked from UIKeyboard which is not a public API as far as I know.
NSNotificationCenter is a class. In Objective-C, classes are declared in header files; this one is in NSNotification.h. (Try pressing Command-Shift-O and typing "NSNotificationCenter" to find this yourself.) When you want to use a class, you #import the header file that the class is declared in; this makes the compiler read that header file first and make all the classes (and other globals) available to your code to use.
This would be a huge pain, though, since a typical Cocoa app uses zillions of classes and other globals from Apple's libraries. So, instead, you just need to #import <Foundation/Foundation.h> which is a header file that just includes a bunch of other header files, including NSNotification.h. (The import statement for Foundation is probably in your own header file, or something else like #import <UIKit/UIKit.h> which also will include foundation and ultimately NSNotification.h.)
One final detail is that there's is probably a "prefix" header in your project which includes UIKit.h in all of your files automatically, so anything declared in there is always available to your code.
NSNotificationCenter is a class in Foundation.
NSNotificationCenter doesn't need to be declared and stored in a variable, its just a call, to explain it further, think of NSNotificationCenter as a tackboard where things get posted, you add a note to that backboard by creating the NSNotificationCenter, and you assingn observers to look at that board, and do something when a note is added.
The NSNotificationCenter object being used in this case is a Singleton. What you need to know is that sending the message defaultCenter to the NSNotificationCenter class object always returns the same NSNotificationCenter object.
Here's what the call to default center might look like
+ (NSNotificationCenter*)defaultCenter
{
static dispatch_once_t once;
static NSNotificationCenter* sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
/*
set up properties, etc
*/
});
return sharedInstance;
}
I have huge issue with my newest game for iPhone made with Games by Tutorials book's help.
NOTE : method from SpriteKit- the right way to multitask doesn't work.
So, in my ViewController.m file, I'm defining private variable SKView *_skView.
Then, I do something like this :
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if(!_skView)
{
_skView = [[SKView alloc] initWithFrame:self.view.bounds];
_skView.showsFPS = NO;
_skView.showsNodeCount = NO;
// Create and configure the scene.
SKScene * scene = [MainMenuScene sceneWithSize:_skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[_skView presentScene:scene];
[self.view addSubview:_skView];
}
}
And I have my _skView defined, and everything works just fine.
But, when I interrupt game, it resets its state to initial, so, for example, if I'm currently playing, and somebody calls me, game switches back to main menu. It can't be like this.
Based on the site I mentioned above, I created this :
- (void)applicationWillResignActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
SKView *view = (SKView *)self.window.rootViewController.view;
view.paused = NO;
}
But game crashes as soon as it is launched, because second method is called and SKView* view is nil.
Getting it from self.window.rootViewController.view doesn't work.
I also tried to get it from self.window.rootViewController.view.subviews, but it doesn't work either.
I can't define my SKView (in ViewController.m) like this :
SKView * skView = (SKView *)self.view;
because then I have errors with my GameCenterController.
Can someone help me how correctly get actual skView and pause it properly??
You could subscribe to these notifications yourself within the ViewController that manages the SKView. Then you don't have to navigate some weird hierarchy to obtain it.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willEnterBackground)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
- (void)willEnterForeground {
self.skView.paused = NO;
}
- (void)willEnterBackground {
// pause it and remember to resume the animation when we enter the foreground
self.skView.paused = YES;
}
I have a situation in which it can happen, that the last strong reference to an observer is removed while the observer processes an incoming notification.
That leads to the observer being deallocated immediately. I would normally expect, that a currently running method can finish before an object is deallocated. And this is what happens during normal message dispatch.
A simplified version of the code:
TKLAppDelegate.h:
#import <UIKit/UIKit.h>
#import "TKLNotificationObserver.h"
#interface TKLAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) TKLNotificationObserver *observer;
#end
TKLAppDelegate.m:
#import "TKLAppDelegate.h"
#implementation TKLAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create an observer and hold a strong reference to it in a property
self.observer = [[TKLNotificationObserver alloc] init];
// During the processing of this notification the observer will remove the only strong reference
// to it and will immediatly be dealloced, before ending processing.
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationName" object:nil];
// Create an observer and hold a strong reference to it in a property
self.observer = [[TKLNotificationObserver alloc] init];
// During the manual calling of the same method the observer will not be dealloced, because ARC still
// holds a strong reference to the message reciever.
[self.observer notificationRecieved:nil];
return YES;
}
#end
TKLNotificationObserver.m:
#import "TKLNotificationObserver.h"
#import "TKLAppDelegate.h"
#implementation TKLNotificationObserver
- (id)init {
if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notificationRecieved:) name:#"NotificationName" object:nil];
}
return self;
}
- (void)notificationRecieved:(NSNotification *)notification {
[self doRemoveTheOnlyStrongReferenceOfThisObserver];
NSLog(#"returing from notification Observer");
}
- (void)doRemoveTheOnlyStrongReferenceOfThisObserver {
TKLAppDelegate * delegate = [[UIApplication sharedApplication] delegate];
delegate.observer = nil;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"NotificationName" object:nil];
NSLog(#"dealloc was called");
}
#end
Using the App Delegate in this way is no good style and only done for demonstration purposes, the real code does not involve the app delegate.
The output is:
dealloc was called
returing from notification Observer
returing from notification Observer
dealloc was called
That is in the first case dealloc is called before the notification processing finished. In the second case it behaves as I expected.
If I keep a strong reference to self inside notificationReceived the dealloc only happens after the processing. My expectation was, that ARC, the runtime or whoever else keeps this strong reference for me.
What is wrong with my code?
Or is something wrong with my expectation?
Is there any Apple- or Clang-provided documentation on this?
My expectation was, that ARC, the runtime or whoever else keeps this
strong reference for me.
That is not the case, as documented in the Clang/ARC documentation:
The self parameter variable of an Objective-C method is never actually
retained by the implementation. It is undefined behavior, or at least
dangerous, to cause an object to be deallocated during a message send
to that object.
Therefore, if calling doRemoveTheOnlyStrongReferenceOfThisObserver
can have the side-effect of releasing self, you would have to use
an temporary strong reference to avoid deallocation:
- (void)notificationRecieved:(NSNotification *)notification {
typeof(self) myself = self;
[self doRemoveTheOnlyStrongReferenceOfThisObserver];
NSLog(#"returing from notification Observer");
}
A better solution would probably to avoid this side-effect.
the first dealloc probably happens as you set the observer property of the appDelegate twice and therefore the first instance is dealloced as soon as you set it the second time
I'm a very beginner in iOS developpment. What I what to do is to delete the content of 2 fields (login and password) when application moves to foregroung after being in background.
To be clear: if a user put the application in foreground and is on the login screen, fields corrsponding to login and password should be empty.
What I have done: I have add a listener to the AppDelegate file that is detecting well background/foreground actions. Here is the code:
- (void)applicationWillEnterForeground:(UIApplication *)application {
#try {
UINavigationController *navigationController = (UINavigationController*)self.window.rootViewController;
UIViewController *cont = [[navigationController viewControllers] objectAtIndex:[navigationController viewControllers].count - 1];
if ([cont isKindOfClass:[LoginScreenController class]]){
NSLog(#"[AppDelegate] ok, we're on login screen");
}
else {
NSLog(#"[AppDelegate] No, we're not");
}
}
#catch(NSException *exp)
{
NSLog(#"[AppDelegate] Fail: %#",exp);
}
}
But when executing the code, I reach an issue linked to BaseRootView... First, do I proceed the appropriate way and then, how to do what I want to do? That's to say, how to erease the fields (I have a function to do this in the LoginController class, so how to call it prperly?)
Thanks !
Put your code in
- (void)applicationDidBecomeActive:(UIApplication *)application
You should use this method instead of
- (void)applicationWillEnterForeground:(UIApplication *)application
when you update user interface this is what this method is for.
Hope this help.
// EXTENDED
Can you try put this code to UIApplicationDidBecomeActiveNotification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(updateUI)
name:UIApplicationDidBecomeActiveNotification
object:nil];
And add this code to AppDelegate:
-(void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}
and add:
-(void)updateUI
{
//Add code to update ui
}
You can do this by posting a notification. Here in the place of yourFunctionName write your function name which is to erase the fields
In login view controller write this following code.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(yourFunctionName) name:#"removeText" object:nil];
}
return self;
}
Now post a notification in applicationWillEnterForeground method.
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"removeText" object:nil];
}
Hope this helps you.
I used to have my UIView that worked fine with several delegates some of them below. Now I changed that UIView from IB to be a UIScrollView (Now used as main view).
Since I've changed to UIScrollView my event delegates such as those below dont work anymore. Such as keyboard and also I had an element that I could move around and not it is just static.
I assigned all delegates form the IB that I could think of and did most things that I know. but i am running out of ideas on why the events are not getting triggered....
if i go back to the old UIView by doing cmd + z they work.
Could anyone point me in the right direction??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
[self.view endEditing:TRUE];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
// register for keyboard notifications
return YES;
}
EDIT - Complementary answer:
#Wezly Answer is totally valid.
But if anyone doesn't want to subclass the UIScrollView and use only UITextFieldDelegate methods.
another way of doing it is adding to viewDidLoad:
Note: you still cant access many things, but its another workaround
///
/// DelegateNotifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(textFieldDidEndEditing:)
name:UITextFieldTextDidEndEditingNotification
object:self.view.window];
I suggest that you subclass your UIScrollView object and add your touch events inside of it like below..
canvasObject.h
#import <UIKit/UIKit.h>
#interface canvasObject : UIScrollView
#end
canvasObject.m
#import "canvasObject.h"
#implementation canvasObject
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.canCancelContentTouches = false;
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event
{
//Do Stuff
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
return YES;
}
#end
Then link your UIScrollView using the identity inspector in interface builder to the new scroll view subclass like below..