NSWindow event when change size of window - objective-c

what is method call when change size in Window?
I find somesing aboud windowDidResize: so i try doing
- (void)windowDidResize:(NSNotification *)notification {
NSLog(#"test");
}
I found what need use NSWindowDidResizeNotification, but I work for the first time with NSNotification and bad understand about this.
Can somebody write a full example for my event, please?

The -windowDidResize: method is called on the window delegate. Is the object with the method you posted the delegate for the window?
For something other than the delegate, you can do:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidResize:) name:NSWindowDidResizeNotification object:theWindow];
and, when the observer is no longer interested or being deallocated:
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:theWindow];
Another approach is to use the new block-based API to NSNotificationCenter:
id observation = [[NSNotificationCenter defaultCenter] addObserverForName:NSWindowDidResizeNotification object:theWindow queue:nil usingBlock:^(NSNotification *){
NSLog(#"test");
}];
// store/retain the observation for as long as you're interested in it. When it's deallocated, you stop observing.

You can Implement NSWindowDelegate:
class YourVC: NSWindowDelegate {
// This method trigger when you press the resize button in the window toolbar
func windowDidResize(_ notification: Notification) {
// Write your code here
}
}
And, In viewDidLoad() or viewDidAppear() method
self.view.window?.delegate = self
You can also use other delegate methods:
windowDidEnterFullScreen
windowDidExitFullScreen
...

Related

How to close a modal view when the application enter in background on ios

I have a modal view created in a method (there is no reference in the mainview) and I want to do a dismissModalViewControllerAnimated automatically when my app enter in background. How can I do that ?
In the mainview's viewDidLoad, add observer to be notified when app goes to background.
- (void) viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(goToBackground)
name:UIApplicationWillResignActiveNotification object:nil];
}
Define the function goToBackground(). It will be called when the app goes to background
- (void) goToBackground
{
[self dismissModalViewControllerAnimated: NO]; // no need to animate
}
Don't forget to remove the observer
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
You can use a notification. Post a notification from the ApplicationDelegate's method applicationDidEnterBackground:. YOu can call the dismiss method from the modal controller, so add it as observer to the notification center.

Removing a NSNotificationCenter observer in iOS 5 ARC

I have an iOS 5 ARC-based project, and am having difficulty about where I should be removing the observer for the NSNotificationCenter observations which I have registered within a UIViewController. Similar posts on SO have said this should be done in the -dealloc method. Even though this method is not required in ARC projects I have added it with the following code:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
As a test, I open the UIViewController (within a UINavigationController), do some things which trigger the notifications, and then pop it off the stack by tapping the Back button. I then reopen the UIViewController, and do some more things to trigger the notifications, but notice that each callback is being called twice - an indication that the previous notifications have not been deregistered. Repeating this procedure just causes each callback to be called more than more times, so they appear to never be deregistering.
Any help would be appreciated!
It's pretty clear your dealloc method isn't being called (nor is the removeObserver call).
Why not remove your UIViewController's observer in the viewDidUnload: or viewWillDisappear: methods?
If your dealloc isn't being called, it's likely because someone is still holding a reference to the view controller. Perhaps you need to mark something as __weak? You can use the allocations instrument to help track down what's holding on to your view controller.
"I also need the notification callbacks to still be fired if the view is off-screen" -> you may need to register UIApplicationWillEnterForegroundNotification. If so, let try this:
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"viewWillAppear");
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"viewWillDisappear");
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"applicationWillEnterForeground");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
// do your stuff here
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(#"applicationDidEnterBackground");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
}
The idea is adding or removing UIApplicationDidEnterBackgroundNotification whenever coming in and out of your screen. We just register UIApplicationWillEnterForegroundNotification when the app enter background and remove once it's back. Be noticed that we just remove UIApplicationDidEnterBackgroundNotification when viewWillDisappear.
My dealloc() is not called by somehow, so I found this way, hope it useful for you too.
Enjoy :)

How to call a selector in a different class?

I'm trying to use this code but Xcode returns an error because the method I'm trying to call in the selector:#selector() is in another class. Thanks for your help!
AppDelegate.m:
-(void)applicationDidBecomeActive:(UIApplication *)application{
[..]
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethodHere) name:UIApplicationDidBecomeActiveNotification object:nil];
}
MainViewController.m:
-(void)myMethodHere{
[..]
}
The problem is that you use
addObserver:self
which means that it looks for the function in the current class. Instead do something like
addObserver:instanceOfOtherClass
Update
Add the call to the init method of MainViewController
// MainViewController.m
- (id)init;
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(someMethod) name:UIApplicationDidBecomeActiveNotification object:nil];
}
return self;
}
Make sure to remove yourself in dealloc
- (void)dealloc;
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
By doing it this way from the very moment the object comes in to existence it is ready to receive notifications and then when it is being deallocated it will safely remove itself.
A good pattern to follow is to make the class that is doing the observing responsible for registering for notifications. This keeps encapsulation well and removes some risk of sending notification to deallocated instances.
Rationale
You need to balance your calls for registering for notifications and unregistering for notifications otherwise a message may be called on a deallocated object which could be hard to track down.
If I have a class that needs to be notified of an event the likely hood is I will register for the notifications in the init method and then unregister for the notifications in the dealloc (init and dealloc are just examples of times I often do this, not necessarily the best place in every example, do what makes sense in your case).
The issue is your use of
addObserver:self
The observer needs to be an instance class that contains the method you want to call, so create that first and then add the notification. Something like.
-(void)applicationDidBecomeActive:(UIApplication *)application{
[..]
SomeClass *newObject = [[SomeClass alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:newObject selector:#selector(someMethodContainedInSomeclass) name:UIApplicationDidBecomeActiveNotification object:nil];
}

Send NSNotification from classA to classB

So i have an app with an In App purchase. The In App purchase is managed in FirstViewController. When the user has purchased the product, i want to send out a Notification to my MainTableViewController to reload the tables data and show the new objects that were purchased in the In App purchase. So basically i want to send a notification from class A to class B and class B reloads the data of the tableview then. I have tried using NSNotificationCenter, but with no success, but i know that its possible with NSNotificationCenter i just don't know how.
In class A : post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"DataUpdated"
object:self];
In class B : register first for the notification, and write a method to handle it.
You give the corresponding selector to the method.
// view did load
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleUpdatedData:)
name:#"DataUpdated"
object:nil];
-(void)handleUpdatedData:(NSNotification *)notification {
NSLog(#"recieved");
[self.tableView reloadData];
}
Ok I'm adding a little bit more information to vince's answer
In class A : post the notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"DataUpdated"
object:arrayOfPurchasedObjects];
In class B : register first for the notification, and write a method to handle it.
You give the corresponding selector to the method. Make sure your class B is allocated before you post the notification otherwie notification will not work.
- (void) viewDidLoad {
// view did load
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleUpdatedData:)
name:#"DataUpdated"
object:nil];
}
-(void)handleUpdatedData:(NSNotification *)notification {
NSLog(#"recieved");
NSArray *purchased = [notification object];
[classBTableDataSourceArray addObjectsFromArray:purchased];
[self.tableView reloadData];
}
- (void) dealloc {
// view did load
[[NSNotificationCenter defaultCenter] removeObserver:self
name:#"DataUpdated"
object:nil];
[super dealloc];
}
Maybe you trying to send notification from another thread? NSNotification won't be delivered to the observer from another thread.

How to reloadData?

I am not dumb and I know how to reload data. I am in a tricky situation where I have a UIView inside another UIView both named OHGridView. I have to keep them named the same way.
With the OHGridView example code, the refresh looked a little like this:
[(OHGridView *)self.view reloadData];
But now that I added a UIView, it no longer works.
Any help is appreciated!
Edit:
Code removed
The NSNotificationCenter may be what you need. You can register for events (eg a perform update event) and then post these events from anywhere. These go to the notification center and then to your class/view. When the event is received, you just do what is needed.
The docs are here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
Inside the OHGridView you would call during initialization:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ReloadNotification:) name:#"ReloadOHGridView" object:nil];
Then, just define the method:
- (void)ReloadNotification:(NSNotification *)notification
{
[self reloadData];
}
So, when you want an update to occur, you then just call:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ReloadOHGridView" object:self];
When you deallocate the OHGridView you should remove the observer:
[[NSNotificationCenter defaultCenter] removeObserver:self];