I have a UISwitchcontroller in my setting page to change the image in main page. So I put a notification from setting page to main page. But only one notification I active every time, my setting page switch code look like this:
-(IBAction)_clickswitchlowlight:(id)sender
{
if(switchControll.on){
[switchControll setOn:YES animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlighton object:nil];
}
else{
[switchControll setOn:NO animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlightoff object:nil];
}
}
In my main page.h..I write this code:
extern NSString * const NOTIF_lowlighton;
extern NSString * const NOTIF_lowlightoff;
In .m above the implementation of main page I write this:
NSString * const NOTIF_lowlighton = #"lowlighton";
NSString * const NOTIF_lowlightoff = #"lowlightoff";
In viewwillappear I write this code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(clicklowlighton:) name:#"lowlighton" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(clicklowlightoff:) name:#"lowlightoff" object:nil];
}
Then this code for changing the image:
- (void)clicklowlighton:(NSNotification *)notif
{
[[self multiPageView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"bgipad"]]];
}
- (void)clicklowlightoff:(NSNotification *)notif
{
[[self multiPageView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"bglowlight#2x"]]];
}
I only get the clicklowlightoff notification, I didn't get the first notification...any missing in my code?
bind your function to event: UIControlEventValueChanged to this function
-(IBAction)_clickswitchlowlight:(id)sender
{
if(switchControll.on){
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlighton object:nil];
}
else{
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlightoff object:nil];
}
}
Related
I'm currently trying to get NSNotification to work but I'm having some trouble.
I have two (2) ViewControllers: A. MainViewController & B. LoginViewController.
In my MainViewController I have a logout button that will send a url to my LoginViewController to load it (without showing my loginView). However, it's not working.
In my MainViewController this is what I have:
- (IBAction)logout:(id)sender {
NSURL *logoutURL = [NSURL URLWithString:#"https://myurl.com/logout"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"logoutInitiated" object:logoutURL];
}
This is what I have in my LoginViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
WebView.delegate = self;
WebView.scalesPageToFit = YES;
WebView.multipleTouchEnabled = YES;
loadCount = 0;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(submitLogout) name:#"logoutInitiated" object:nil];
}
- (IBAction)submitLogout:(NSNotification*)notification {
[WebView stopLoading];
NSURL * signOutUrl = (NSURL*)[notification object];
[self loadURL:nil withURL:signOutUrl];
}
My problem is that when I press the logoutButton nothing happens. (Using NSLogs, I see that it never triggers the next step) Thank you!!!!
This is because your method name you are passing in selector is wrong. You need to add colon : at submitLogout: suffix
Use
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(submitLogout:) name:#"logoutInitiated" object:nil];
in place of
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(submitLogout) name:#"logoutInitiated" object:nil];
Hope it helps you.
When you add self as an observer, you use the selector "submitLogout" without a semicolon! But your method has an argument, so the correct selector would be #selector(submitLogout:).
Note the SEMICOLON
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(**submitLogout:**) name:#"logoutInitiated" object:nil];
- (IBAction)logout:(id)sender
{
NSURL *logoutURL = [NSURL URLWithString:#"https://myurl.com/logout"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"logoutInitiated" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:logoutURL,#"RECEIVED_URL", nil]];
}
- (IBAction)submitLogout:(NSNotification*)notification
{
[WebView stopLoading];
NSURL * signOutUrl = (NSURL*)[notification objectForKey:#"RECEIVED_URL"];
[self loadURL:nil withURL:signOutUrl];
}
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 do I find out if the keyboard is up?
I have a UISearchbar instance which becomes the first responder.
When the keyboard appears a notification is sent out as part of the API, however I don't want to respond to this right away. I could record this in a boolean state, but that seems clunky. I'd like to know if there is a "getter" some where I can call to find out.
This is how I do it:
KeyboardStateListener.h
#interface KeyboardStateListener : NSObject {
BOOL _isVisible;
}
+ (KeyboardStateListener *) sharedInstance;
#property (nonatomic, readonly, getter=isVisible) BOOL visible;
#end
KeyboardStateListener.m
#import "KeyboardStateListener.h"
static KeyboardStateListener *sharedObj;
#implementation KeyboardStateListener
+ (KeyboardStateListener *)sharedInstance
{
return sharedObj;
}
+ (void)load
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
sharedObj = [[self alloc] init];
[pool release];
}
- (BOOL)isVisible
{
return _isVisible;
}
- (void)didShow
{
_isVisible = YES;
}
- (void)didHide
{
_isVisible = NO;
}
- (id)init
{
if ((self = [super init])) {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(didShow) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:#selector(didHide) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
#end
Then use this to figure out the rest:
KeyboardStateListener *obj = [KeyboardStateListener sharedInstance];
if ([obj isVisible]) {
//Keyboard is up
}
The only sure way that I can think to do it as you said. using notifications like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
and then
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
Other than that, you may be able to iterate through your views subviews and look for the keyboard like:
UIView *keyboard = nil;
for (UIView *potentialKeyboard in [myWindow subviews]) {
// iOS 4
if ([[potentialKeyboard description] hasPrefix:#"<UIPeripheralHostView"]) {
potentialKeyboard = [[potentialKeyboard subviews] objectAtIndex:0];
}
if ([[potentialKeyboard description] hasPrefix:#"<UIKeyboard"]) {
keyboard = potentialKeyboard;
break;
}
}
But I am not sure if this will break when the SDK changes ...
Maybe use this method and add a category to the window so that you can just always ask the window for the keyboard ... just a thought.
How to implement to get UIAccessibilityVoiceOverStatusChanged Notification?
I tried like below but nothing happens :
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(notified:) name:UIAccessibilityVoiceOverStatusChanged object:self];
That looks reasonable, except maybe object:self should be object:nil?
The other thing is to be sure your signature is correct:
- (void)voiceOverStatusChanged: (NSNotification *)notification;
I think you might try adding the observer in the awakeFromNib method with the right selector signature.
Something like this will work
- (void)awakeFromNib {
[super awakeFromNib];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(voiceOverChanged)
name:UIAccessibilityVoiceOverStatusChanged
object:nil];
[self voiceOverChanged];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIAccessibilityVoiceOverStatusChanged object:nil];
}
- (void)voiceOverChanged {
// Your actions here
}
you can get the UIAccessibilityVoiceOverStatusChanged Notification with the code
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(didChangeVoiceOverStatus:)
name:UIAccessibilityVoiceOverStatusChanged
object:nil];
}
- (void)didChangeVoiceOverStatus:(NSNotification *)notification {
if (UIAccessibilityIsVoiceOverRunning()) {
NSLog(#"VoiceOver is ON.");
} else {
NSLog(#"VoiceOver is OFF.");
}
}
Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *musicURL = [NSURL URLWithString:#"http://live-three2.dmd2.ch/buureradio/buureradio.m3u"];
if([musicURL scheme])
{
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:musicURL];
if (mp)
{
// save the music player object
self.musicPlayer = mp;
[mp release];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popBack:) name:#"MPMoviePlayerDidExitFullscreenNotification" object:nil];
// Play the music!
[self.musicPlayer play];
}
}
}
-(void)popBack:(NSNotification *)note
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
The selector method never gets called. I just want to pop back to the root menu when the "Done" button is pressed on the movie player. I put an NSLog in the selector to check if it was even being called, nothing. The music plays fine. Any thoughts?
This should work
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];