Notification not being recognised - iphone

I am trying to post a notification without much success! I can do it
ok for the keyboard without issue but now am trying a custom one as
follows:
In my rootview I have this
.h
-(void) allowEdits:(NSNotification *)notification;
.m
//this section is run in method to present the passcode entry form
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(allowEdits:) name: #"PasscodeOK" object:nil];
PasscodeEntryViewController *vc = [[PasscodeEntryViewController alloc]
init];
[self presentModalViewController: vc animated:YES];
[vc release];
// and this is the response to the notification
-(void) allowEdits:(NSNotification *)notification {
NSLog(#"notification fired");
}
// in the vc instance I have this to notify passcode was ok
[[NSNotificationCenter defaultCenter]
postNotificationName:#"PasscodeOK" object:nil];
[self dismissView];
But the allowEdits never gets called?

Could you try posting your notification with:
[[NSNotificationCenter defaultCenter] postNotificationName:#"PasscodeOK" object:self];
As sender take the vc instance (self) instead of nil. Maybe that's solving your problem.

Related

Present another View Controller from SkScene

I'm trying to present another viewController from my "SkScene".
This is my main viewController(tuViewController)
Code:
-(void) openTweetSheet{
FacebookLikeViewDemoViewController *ctrl = [[FacebookLikeViewDemoViewController alloc] initWithNibName:#"FacebookLikeViewDemoViewController" bundle:nil];
[self presentViewController:ctrl animated:YES completion:nil];
}
This is my "SkScene":
tuViewController *viewController = [[tuViewController alloc]init];
[viewController openTweetSheet];
And the viewController which i want to present is FacebookLikeViewDemoViewController and i need to have way back to "SkScene".
And i got sigabrt error, i tried few ways to present viewController but always with failure, one time i got swap to viewController but it was entirely black. I read a lot how to perform that, but i personally can't figure out. I appreciate your help.
I tried too with Notification Center.
Main viewController
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(goToGameOverViewController:)
name:#"GoToGameOverViewController"
object:nil];
-(void)goToGameOverViewController:(NSNotification *) notification {
FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:#"HelpViewController" bundle:nil];
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:helpVC animated:YES completion:nil];
}
SkScene
[[NSNotificationCenter defaultCenter]
postNotificationName:#"GoToGameOverViewController" object:self];
But i got the same error. I prefer to figure out why the way with notification won't work.
I assume by your question that you are looking to do some social media posting.
You can either pass a reference for your View Controller to your SKScene or you can use NSNotificationCenter instead. I prefer to use the latter.
First make sure you have added the Social.framework to your project.
Import the social framework into your View Controller #import <Social/Social.h>
Then in your View Controller's viewDidLoad method add this code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(createTweet:)
name:#"CreateTweet"
object:nil];
Next add this method to your View Controller:
-(void)createTweet:(NSNotification *)notification
{
NSDictionary *tweetData = [notification userInfo];
NSString *tweetText = (NSString *)[tweetData objectForKey:#"tweetText"];
NSLog(#"%#",tweetText);
// build your tweet, facebook, etc...
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
At the appropriate location in your SKScene, (won game, lost game, etc...) add this code:
NSString *tweetText = #"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:tweetText forKey:#"tweetText"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"CreateTweet" object:self userInfo:userInfo];
The above code sends a NSNotification, with text, which your View Controller will pick up and execute the specified method (which is createTweet in the above example).

NSNotificationCenter previously working in iOS 6.1 - iOS 7 issue?

I have the following snippet of code in viewDidLoad which I post a notification to be received by a different ViewController, and prior to iOS 7 (and XCode 5) this has worked:
if ([PFUser currentUser] && [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
NSLog(#"Current user exists and is logged in"); //current works, so I know that this if-statement is satisfied
[self performSegueWithIdentifier:#"GoToRatingsView" sender:self];
[[NSNotificationCenter defaultCenter] postNotificationName:#"testSetup" object:nil]; //part in question
}
And in the segued ViewController, I have the following:
(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(testSetupFunction) name:#"testSetup" object:nil];
}
(void)testSetupFunction
{
NSLog(#"This function executed");
}
Currently, testSetupFunction does not executed, which means the notification is not received. I am unsure whether its because I have segued to a different view and then posted the notification, or that this is something new with iOS 7, but currently the notification is no longer received.
Thanks for your help!
Post the notification once the viewcontroller has time to load.
- (void)postNotificaiton
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"testSetup" object:nil];
}
if ([PFUser currentUser] && [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
[self performSegueWithIdentifier:#"GoToRatingsView" sender:self];
[self performSelector:#selector(postNotification) withObject:nil afterDelay:0.0];
}
You could also post a notification from your segued View Controller saying that it was loaded, and in response to that notification, post your testSetup notification.
did you try adding the observer in the init method
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(testSetupFunction) name:#"testSetup" object:nil];
// Custom initialization
}
return self;
}
OR You can also try to put a log in viewDidLoad to check actually the view is loaded at that time, sometimes it takes time to load the view, view is only loaded only when it is needed.

Reload table not working when app comes to foreground

i want to reload table when app comes to foreground from background, in my app delegate.m i did like this, but its not working
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(#"applicationWillEnterForeground");
[[NSNotificationCenter defaultCenter] postNotificationName:#"EnteredForeground"
object:nil];
}
and in my viewController i am working like
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(whenAppEnteredIntoForeground:) name:#"EnteredForeground" object:nil];
}
- (void)whenAppEnteredIntoForeground:(id)object {
NSLog(#"log msg");
[tblSearch reloadData];
}
what should i do? what mistake i am doing? any help please
Firstly, you don't need to rebroadcast a notification when the app comes to the foreground, you can register for the notification from your view controller.
In your case, it's likely to be that the view is not loaded until after your secondary notification is sent, which is why your view controller cannot respond to it. Using breakpoints will confirm if this is the case.
Use this instead:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(whenAppEnteredIntoForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
And you don't need to rebroadcast a notification from your appdelegate.
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appEnteredIntoForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)appEnteredIntoForeground:(id)object {
[tableView reloadData];
}
Make sure that your Reload Data must call from main Thread Otherwise it will not be reloaded.

NSNotification not receiving in View Controller from appDelegate

hello i assign nsnotifiaction in app delegate.m's method and this method call eprox every 30sec, and i wants its notifcation in viewcontroller adn execute method,
here is my code of appdelegate .m
- (void)layoutAnimated:(BOOL)animated{
BOOL yy= self.bannerView.bannerLoaded;
if (yy==1){
self.iAdString=[NSMutableString stringWithString:#"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionWillBegin" object:self];
}
else{
self.iAdString=[NSMutableString stringWithString:#"0"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionDidFinish" object:self];
}
}
and in viewcontroller.m
//i defined in viewdidload method
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishBannerViewActionNotification:) name:#"BannerViewActionDidFinish" object:nil];
}
its method are..
- (void)willBeginBannerViewActionNotification:(NSNotification *)notification{
[self.view addSubview:self.app.bannerView];
NSLog(#"come");
}
- (void)didFinishBannerViewActionNotification:(NSNotification *)notification {
NSLog(#"come");
[self.app.bannerView removeFromSuperview];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
I have not getting response of excessing method while method read in appdelegate file.
Please help me.
You have a typo error.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
//Your error here-------------------------------------------------------------------------------------------------------------------------------------^
You have put a space there.
SideNote: For all notification names, you should/can create a separate file and put all your notification names as constants strings.
const NSString *kBannerViewActionWillBegin=#"BannerViewActionWillBegin";
this will be easier to change the value and no such typo will happen.
From your code what I get is except for the notification name's all other stuff is fine, did you check whether the notification gets fired. Try keeping break points at the notification firing line.

How to present a modal view controller when the app enters in foreground?

I'm trying to present a Modal View Controller when the app enters in foreground.. These are my files:
AppDelegate.m :
#import "AppDelegate.h"
#import "MainViewController.h"
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[self.window makeKeyAndVisible];
MainViewController * vc = [[MainViewController alloc]init];
[vc myMethodHere];
}
MainViewController.h :
//[..]
-(void) myMethodHere;
MainViewController.m :
-(void)myMethodHere{
NSLog(#"myMethodHere Activated.");
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[self presentModalViewController:tweetViewController animated:YES];
}
NSLog(#"myMethodHere Activated.") works.. so I can't understand why "presentModalViewController" doesn't! What should I edit/add? Maybe a delay? Thanks for your help..
p.s. I know my english sucks.. Forgive me :)
I wouldn't rely on the methods in your app delegate for this (even though it seems like the obvious solution) because it creates unnecessary coupling between your application delegate and the view controller. Instead, you can have MainViewController listen for the UIApplicationDidBecomeActive notification, and present the tweet composer view controller in response to this notification.
First, register for the notification in -viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethodHere) name:UIApplicationDidBecomeActiveNotification object:nil];
}
Now, when this notification is received when your app returns from the background, myMethodHere will be invoked.
Lastly, remember to remove yourself as an observer when the view unloads.
- (void)viewDidUnload
{
[super viewDidUnload];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}