I wanted to support keyboard for my SpriteKit game, but here's the problem:
We get the command keys from ViewController:
- (NSArray *)keyCommands {
return #[[UIKeyCommand keyCommandWithInput:#" " modifierFlags:0 action:#selector(fire)]];
}
But the game logic are all in SKScene, presented from the ViewController... and there are multiple SKScene... how does the scene get the command from ViewController? Or we need to do keyboard polling?
You can send message by NSNotification
Add Observer in SKScene,
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(selectorMethod:)
name: #"NOTIFICATION_NAME"
object:nil];
Post Notification form UIViewController,
NSDictionary *userInfo = #{ #"Key": #"Value" };
[[NSNotificationCenter defaultCenter] postNotificationName: #"NOTIFICATION_NAME" object:nil userInfo:userInfo];
Related
In my custom UITableViewCell I added an observer in NSNotificationCenter:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(stopActivityIndicator) name:#"stopActivityIndicator" object:nil];
I post a notification in a UIViewController:
[[NSNotificationCenter defaultCenter] postNotificationName:#"stopActivityIndicator" object:nil];
This function "stopActivityIndicator" is not being called, any idea what causes this?
EDIT:
ExploreViewController.m
-(void)showCorrectBannerAfterPlusButtonClicked:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"stopActivityIndicator" object:nil];
}
ExploreViewController contains a UITableView with ExploreTableViewCells.
ExploreTableViewCell.m
- (IBAction)plusButtonClicked:(id)sender
{
self.plusButton.hidden = YES;
[self.plusButtonActivityIndicator startAnimating];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(stopActivityIndicator) name:#"stopActivityIndicator" object:nil];
}
-(void)stopActivityIndicator
{
_plusButton.hidden = NO;
[self.plusButtonActivityIndicator stopAnimating];
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"stopActivityIndicator" object:nil];
}
Ok, question, when do you call "showCorrectBannerAfterPlusButtonClicked"?? As this is the method where you post the notification observed by the tableViewCell.
What I see is the notification observer adding and removal, but I don't see how the UIViewController knows when the cell's "plusButtonClicked" is called, so perhaps the method posting the notification is not being called.
Also, be careful with the cell reusage, have in mind if you should remove the observer at that point.
I am using NSNotification to pass value between ViewControllers, but value is not getting passed and when i used breakpoints to check what's going wrong i came to know that the receive notification selector method is not called.
Following is the code what i have written
AViewController.m
[[NSNotificationCenter defaultCenter] postNotificationName:#"speciality" object:nil userInfo:[specialityIdArray objectAtIndex:indexPath.row]];
BViewCOntroller.m
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveSpecialityId:) name:#"speciality" object:nil];
}
-(void)receiveSpecialityId:(NSNotification *)notificaton
{
NSString *selectedServiceString=[[notificaton userInfo] valueForKey:#"service"];
_specialtiyId = selectedServiceString;
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"service" object:nil ];
}
I am trying to pass value from AViewController to BViewController
I have read all the discussions done previously on this same issue, but none of them solved my issue
Dont' add
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(receiveSpecialityId:) name:#"speciality" object:nil];
in the viewDidAppear. I don't know much objective c but here's the deal in swift:
func receiveSpecialityID(notification: NSNotification){
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(<Theclassinwhichthismethodiswritten without the ()).receiveSpecialityID(_:)), name: "Selected", object: nil)
/// DO your thing here..
}
Then do a:
deinit{
NSNotificationCenter.DefaultCenter().removeObserver
}
This should work. Try and let me know!
I am facing an issue with NSNotificationCenter.
I am not able to send message and receive message using NSNotificationCenter in latest ios 8.4 (XCode 6.4)
Please check the following code:
1) I want to send data using first view controller to another view.
so i have written the following code in first viewcontroller:
When user btn clicked method as following :
- (IBAction)btnClicked:(id)sender
{
[self postNotification];
[self performSegueWithIdentifier:#"asGo" sender:self];
}
-(void)postNotification{
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification" object:self];
}
2) In Second view controller i have added observer in ViewWillApper as following :
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(eventListenerDidReceiveNotification:)
name:#"MyNotification"
object:nil];
}
-(void)eventListenerDidReceiveNotification:(NSNotification*)txt
{
NSLog(#"i got notfication:");
}
so eventListenerDidReceiveNotification is not called while come on view.
But i am not getting above log while i come on second vc with navigation
As others have noted, NSNotificationCenter doesn't work like a post office. It only delivers notifications if someone actually listens to them at the moment they arrived. This is the reason your eventListenerDidReceiveNotification method is not being called: you add an observer in viewWillAppear, which is called after the segue (I assume that you're using segues because of the performSegueWithIdentifier method in your code) is finished, so it's definitely called after postNotification has been called.
So, in order to pass data via NSNotificationCenter you have to add an observer before you post a notification.
The following code is completely useless and unnecessarily overcomplicated, you shouldn't do anything like that, but since you keep insisting on using a scheme like this, here you go:
//Didn't test this code. Didn't even compile it, to be honest, but it should be enough to get the idea.
NSString * const SOUselessNotificationName = #"MyUselessNotification";
#pragma mark - FIRST VC
#interface SOFirstVC : UIViewController
#end
#implementation SOFirstVC
NSString * const SOasGoSegueIdentifer = #"asGo";
- (IBAction)btnClicked:(id)sender {
[self performSegueWithIdentifier:SOasGoSegueIdentifer sender:self];
}
-(void)postNotification {
[[NSNotificationCenter defaultCenter] postNotificationName:SOUselessNotificationName object:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifer isEqualToString:SOasGoSegueIdentifer]) {
SOSecondVC *destinationVC = (SOSecondVC *)segue.destinationViewController;
[destinationVC registerToReceiveNotificationsFromObject:self];
[self postNotification];
}
}
#end
#pragma mark - SECOND VC
#interface SOSecondVC : UIViewController
-(void)registerToReceiveNotificationsFromObject:(id)object;
#end
#implementation SOSecondVC
-(void)registerToReceiveNotificationsFromObject:(id)object {
[[NSNotificationCenter defaultCenter] addObserver:self selector:(eventListenerDidReceiveUselessNotification:) name:SOUselessNotificationName object:object];
}
-(void)eventListenerDidReceiveUselessNotification:(NSNotification*)uselessNotification {
NSLog(#"I got a useless notfication! Yay!");
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#end
NSNotificationCenter basically has 3 steps
Adding Observer like [[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(open:) name:#"OpenDetail" object:nil];
Posting Notification [[NSNotificationCenter defaultCenter] postNotificationName:#"OpenDetail" object:self];
Removing Observer [[NSNotificationCenter defaultCenter] removeObserver:self name:#"OpenDetail" object:nil];
I think you are posting your notification and then later adding observer while it's vie versa. You have to add observer first then post notification.
HTH
First you have to setup the data you want to send
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:myObject forKey:#"aKey"];
Then you post it with the data like so:
[[NSNotificationCenter defaultCenter] postNotificationName: #"MyNotification" object:nil userInfo:userInfo];
And finally you read the data off the notification:
-(void)eventListenerDidReceiveNotification:(NSNotification*)notification
{
NSLog(#"i got notification:");
NSDictionary *userInfo = notification.userInfo;
NSString *myObject = [userInfo objectForKey:#"aKey"];
}
I have this really unexplained problem with NSNotification method.
I have been using NSNotificationCenter for a long time but i can't explain why this is happening.
My problem is this,
I have a UITableViewCell subclass where i send a NSNotificationCenter method to the UIViewController when a user taps a button in the cell.
[[NSNotificationCenter defaultCenter] postNotificationName:MOVE_TO_PROGRAM_VIEW
object:self
userInfo:#{INDEX_ROW : [NSNumber numberWithInteger:self.tag]}];
Where the self.tag is the row (for the data model in the controller).
In the controller i register for the notification in viewWillAppear: like so:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(userWantsToGoToProgramView:) name:MOVE_TO_PROGRAM_VIEW object:nil];
I also remove myself in the viewWillDisappear:
[[NSNotificationCenter defaultCenter] removeObserver:self name:MOVE_TO_PROGRAM_VIEW object:nil];
Now in the method for the notification i try to get the userInfo and the row but the notificaiton argument is nil for some reason..
- (void)userWantsToGoToProgramView:(NSNotification *)notification
{
// notification is nil here
// get the index of the video in the feed
NSDictionary *userInfo = notification.userInfo;
NSInteger videoIndex = [userInfo[INDEX_ROW] integerValue];
NSDictionary *videoData = self.feed[videoIndex];
}
Any advice of help will be appreciated
Thanks!
I am using NSNotificationCenter to subscribe to event,
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(useNotificationWithLocation:)
name:#"somename"
object:nil];
And sending from static library,
[[NSNotificationCenter defaultCenter] postNotificationName:#"somename" object:nil userInfo:dictionary];
Everything works as expected, but when I use any of the UI event, like button click, I stop getting notification. I do not have any clue what is happening.
useNotificationWithLocation code,
-(void)useNotificationWithLocation:(NSNotification*)value
{
NSDictionary* dictionary = value.userInfo;
CLLocation *currentLoc = [dictionary valueForKey:CURRENT_LOCATION_NOTIFY];
NSLog(#"useNotificationWithLocation %#",currentLoc);
}
Please suggest what should I do about this.