Obj-C Cocoa Notification NSApplicationDidResignActiveNotification - cocoa

I've a class called AppController.h/m I want to make something when the NSNotificationDidResignActiveNotification is sent.
So i wrote this code in AppController.m:
-(void) initialize(){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidResignActive:)
name:NSApplicationDidResignActiveNotification
object:nil ];
}
and then
-(void) applicationDidResignActive (NSNotification*) note{
NSBeep();
}
The problem is that the method isn't executed and i get this in the Console:
+[AppController applicationDidResignActive:]: unrecognized selector sent to class 0x61c4
I can't get where the problem is: could you help me?
Thank you!

initialize is a class method, not an instance method. I don't know this for sure, but it seems that when using a selector in a class method, it also assumes that selector will be a class method (for good reason). AppController has an instance method called applicationDidResignActive, but not a class method named as such.
Instead of registering for notifications in +initialize, override -init and register there.
- (void)init
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidResignActive:)
name:NSApplicationDidResignActiveNotification
object:nil ];
}

Related

Done button action of MPMoviePlayerViewController

I am using custom MPMoviePlayerViewController.
Like below,
#import <MediaPlayer/MediaPlayer.h>
#interface customVideoViewController : MPMoviePlayerViewController
{
}
#end
I am calling this customVideoViewController class from other class to RUN my video serially in landscape mode, video is running fine but I am not able to catch the action onClick of DONE button of MPMoviePlayerViewController.
Can any one guide me.
this is how I am calling to this view.
customVideoPlayerObj = [[customVideoViewController alloc]initWithContentURL:self.url];
[self presentViewController:customVideoPlayerObj animated:YES completion:nil];
[customVideoPlayerObj.moviePlayer play];
I need to catch the action for DONE button click of this, how to do this...?
here is the observer for the same
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(doneButtonClick:)
name:MPMoviePlayerWillExitFullscreenNotification
object:customVideoPlayerObj];
but control is not coming in doneButtonClick
MPMoviePlayerWillExitFullscreenNotification and MPMoviePlayerDidExitFullscreenNotification fired when the movie player exits fullscreen.
You can use MPMoviePlayerPlaybackDidFinishNotification .

NSTextField and NSTextView : Overlapping Delegate Methods

I have a class which was the delegate for NSTextViews, for which I was interested in the textDidEndEditing: method. I now want it to also be the delegate for NSTextFields. Problem is, they both use the same method for signaling end of text editing.
I tried to "fork" my textDidEndEditing: method to deal with both NSNotifications, but it seems like the latest (NSTextFields) don't trigger any message.
Should I be looking for an inside bug, or it is a known limitation ?
- (void)textDidEndEditing:(NSNotification *)aNotification
{ if ([[aNotification object] isKindOfClass:[NSTextView class]])
{
}
else if ([[aNotification object] isKindOfClass:[NSTextField class]])
{
}
}
Change your method and try below:-
- (void)controlTextDidEndEditing:(NSNotification *)aNotification

How to hide window of UIAgent process with cocoa

I have an UIAgent application with one window. I want to hide/show it from another application.How do I do it with cocoa? Seems like hide/unhide methods of NSRunningApplication doesn't affect UIAgent processes.
Thanks in advance
I solved it with NSDistributionNotifications. In the UIAgent application I add an observer to a #"QuitProcessNotification" (any other name):
[[NSDistributedNotificationCenter defaultCenter]
addObserver:self selector:#selector(quit:)
name:#"QuitProcessNotification"
object:#"com.MyCompany.MyApp"
suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
The callback looks like that:
- (void) quit:(NSNotification *) notification
{
[NSApp terminate:nil];
}
In the main application:
Sending notification:
[[NSDistributedNotificationCenter defaultCenter]
postNotificationName:#"QuitProcessNotification"
object:#"com.MyCompany.MyApp"
userInfo: nil /* no dictionary */
deliverImmediately: YES];
Be sure, that the object parameter is indeed your sender application's bundle identifier.

How do you reload a UIView?

I have a UINavigationController where the user can go back/fourth. When the user goes back, I want that UIView to reload. ( I am actually using an OHGridView ). On my ViewWillDisappear, I do something like this:
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadOHGridView" object:self];
}
So when they go back, it will send a NSNotification to the OHGridView to refresh it's data. It get's called, but it get's an error Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailViewController reloadData]: unrecognized selector sent to instance 0x4b9e9f0
Here's how I set up my NSNotificationCenter (in my DetailViewController):
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ReloadGridNotification:) name:#"ReloadOHGridView" object:nil];
}
- (void)ReloadGridNotification:(NSNotification *)notification{
[database executeNonQuery:#"DELETE * FROM images"];
[items removeAllObjects];
[self reloadData];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Now you would think it would update, but I get that error... Please help!
Coulton
Actually, I wouldn't think that it would update. reloadData isn't the name of a documented method of UIViewController, and you don't seem to have implemented one yourself. I'm not familiar with OHGridView, but I perhaps that's the object to which you want to send the reloadData message.
So you can change the observer that you set up from self to your instance of OHGridView, or you can implement a method in your view controller called reloadData that in turn sends the appropriate reload message to your OHGridView.

Observing object using proxy object

Should I be able to set up an observer on a proxy object, change what the proxy object is pointing to and still be able to observe changes on the real object?
An example might explain this best. Consider the following.
In the header:
#interface MyController : NSObject {
MyWidgetModel * aProxyObject;
}
In an initialization or awake from NIB method:
-(void)awakeFromNib {
// Init the proxy object. Could be as an empty widget
[aProxyObject addObserver:self
forKeyPath:#"widgetName"
options:NSKeyValueObservingOptionNew
context:nil];
}
Some other method that changes the object:
-(void)changeWidget:(MyWidgetModel *)aNewWidget {
aProxyObject = aNewWidget;
}
This doesn't fire any changes in aNewWidget. However, if I move the addObserver to after the assignment as follows, it works:
-(void)changeWidget:(MyWidgetModel *)aNewWidget {
[aProxyObject removeObserver:self forKeyPath:#"widgetName"];
aProxyObject = aNewWidget;
[aProxyObject addObserver:self
forKeyPath:#"widgetName"
options:NSKeyValueObservingOptionNew
context:nil];
}
I am assuming that the first case doesn't work is because the observer is observing the memory pointer of the proxy object's reference and, as there is no object at the time the proxy observer is added has nothing to observe. However, if I init a widget and observe that, then assign the proxy object aNewWidget it still doesn't observe changes unless I add the observer after the assignment (and of course creating a need to remove the observer on a change of object).
Also, what happens in this scenario if aNewWidget gets destroyed? Because the observer is on the proxy, does this negate the need to remove the observer before destroying the object? (I assume it doesn't).
Ideally I'd like to be able to set the observer on the proxy and swap in and out whatever widget reference I want to the proxy object without having to worry about adding and removing the observer unless the MyController class goes away in which case I could handle the observer removal in the dealloc.
Any help/comments/advice appreciated.
The keyPath must be KVC compliant. So here's the code:
#interface MyController : NSObject {
MyWidgetModel * aProxyObject;
}
#property (nonatomic, retain) MyWidgetModel * aProxyObject;
Don't forget to synthetize it in the implementation file. Then use this code to add the observer:
[self addObserver:self
forKeyPath:#"aProxyObject"
options:NSKeyValueObservingOptionNew
context:nil];
Please check my edit. I've changed the assign to retain. Maybe it is better for you. You should try to choose the best for you. I just want to say it doesn't matter in KVO.

Resources