NSWindow is not receiving any notification when it loses focus - objective-c

I have a custom NSWindow class that has the following methods:
- (void)setupWindowForEvents{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidResignKey:) name:NSWindowDidResignMainNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowDidResignKey:) name:NSWindowDidResignKeyNotification object:self];
}
-(void)windowDidResignKey:(NSNotification *)note {
NSLog(#"notification");
[self close];
}
I call [_window setupWindowForEvents]; but the windowDidResignKey never gets called.
This is how I call my NSWindow: when the status bar item is clicked I makeKeyAndOrderFront and the Window is displayed right beneath the status bar item, like this:
Any ideas why the I don't get any notification when the window loses focus? I've used both NSWindowDidResignMainNotification and NSWindowDidResignKeyNotification to see if any of these worked, but none is working.

You're probably not getting the notification because you actually are never key in the first place. Your window appears to be borderless, and borderless windows don't grab key window status by default.
In your window subclass, be sure to return YES on the following methods:
- (BOOL)canBecomeKeyWindow {
return YES;
}
- (BOOL)canBecomeMainWindow {
return YES;
}

Related

How to change set title uibutton?

UIButton *loginButton = [self.loginViewController LoginButton];
loginButton.titleLabel.text=#"Log out";
//[loginButton setTitle:#"Log out" forState:UIControlStateNormal];
NSLog(#"Log in :-%#",loginButton.titleLabel.text);
1) i have view controller file that has one button and i want change button title when didFinishedLaunching method called from app-delegate.
i also initialized the controller but that has no change.
thank in advance..
To set the title correctly:
[loginButton setTitle:#"Log Out" forState:UIControlStateNormal];
edit: If you are looking to update view states after a change from the AppDelegate, it would be a good idea to look at using NSNotifcationCenter. In the app delegate, you can post a notification about the user logging in or out, and then you can configure your viewController to be an observer for the notification and update its state when the notification is made.
For example, in your app delegate
- (void)userDidLogOut
{
//This method would be called when you logout
[[NSNotificationCenter defaultCenter] postNotificationName:#"didLogoutNotification" object:nil];
}
Then in your loginViewController
- (void)viewDidLoad
{
//...
//Become an observer of `didLogoutNotification`.
[[NSNoficationCenter defaultCenter] addObserver:self selector:#selector(didLogoutNotification:) name:#"didLogoutNotification" object:nil];
}
- (void)dealloc
{
//...
//Remove yourself from the observation list.
[[NSNoficationCenter defaultCenter] removeObserver:self];
}
- (void)didLogoutNotification:(NSNotification *)notification
{
//...
//Update the button
[loginButton setTitle:#"Log In" forState:UIControlStateNormal];
}

Calling UIKeyboard method on UITextView and not UITextFields

I have view with a UITextView for comments on the bottom. Since its on the bottom of the page, whenever someone edits it, a method is called to shift the view of the page, so that you can still see the comment box. My problem is that the same method also gets called when user are editing UITextFields.
Here's what I have. First I declare a notification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
Then the method itself
- (void)keyboardWillShow:(NSNotification *)notif{
...
}
My first thought was to add a condition, to check and see if the object was a TextView, and only then execute the code. But since I am not passing the object to the method, is there anyway to tell the method what type of object I am dealing with
Text fields and text views also send notifications. In the textFieldShouldBeginEditing and the textViewShouldBeginEditing implementations you could set a flag that you can read in your implementation of the keyboardWillShow method -- the keyboard notification is sent after the text field or text view notifications.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
self.sender = #"text field";
return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
self.sender = #"text view";
return YES;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
}
- (void)keyboardWillShow:(NSNotification *)notif{
NSLog(#"%#",self.sender);
}

NSWindow event when change size of window

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
...

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.

Remove the "Dismiss keybard" key from a UITextView's keyboard on iPad

I have a modal view that with a UITextView, and the user can enter some text (or not) and close the modal view. There is no point of dismissing the keyboard as it does not dismiss the modal view (this is on purpose), and the UITextView without the keyboard just looks silly.
Is there a way to hide or remove the "Dismiss keyboard" key from the keyboard?
You can't hide or remove the key, but you can disable it using the UITextViewDelegate protocol:
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
return NO;
}
If UIKit forces the responder to resign despite the delegate (doubtful, but I haven't looked closely at the call stack), you can force the keyboard to stay up by observing the UIKeyboardDidHideNotification and setting the first responder back to the UITextView: [myUITextView becomeFirstResponder]
Protocol method doesn't help.
As Answerbot suggested one should listen to keyboard notification.
- (void)onIpadViewWillAppear
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showIpadKeyboard) name:UIKeyboardDidHideNotification object:nil];
}
- (void)onIpadViewWillDisappear
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)showIpadKeyboard
{
[myTextField becomeFirstResponder];
}
This works and looks beautifully.