开发者

Notification for keyboard fires also notification for defaults

开发者 https://www.devze.com 2023-03-29 05:38 出处:网络
I have this code in my ViewController: - (void) viewWillAppear :(BOOL)animated { // register for keyboard notifications

I have this code in my ViewController:

- (void) viewWillAppear :(BOOL)animated {
     // register for keyboard notifications
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) 
           name:UIKeyboardDidShowNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) 
           name:UIKeyboardWillHideNotification object:self.view.window];

     // register for defaults change notification
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged:)      
           name:NSUserDefaultsDidChangeNotification object:开发者_开发技巧nil];
 }

 -(void) viewWillDisappear :(BOOL)animated {
     [[NSNotificationCenter defaultCenter] removeObserver:self];
 }

The problem is that when the keyboard appears, method defaultsChanged also is fired.

When keyboard appears, this is the sequence of calls (extract from the log):

start defaultsChanged 
end   defaultsChanged
start textFieldDidBeginEdit
end   textFieldDidBeginEdit
start keyboardWasShown
end   keyboardWasShown

I do not know what can be. Anyone have any ideas?


Perhaps the defaults really did change as a side effect of keyboard being shown? Why don't you print out the defaults before and after and see if anything changed? Here is some code to print NSUserDefaults with NSLog:

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSDictionary* defaultsDict = [defaults dictionaryRepresentation];
NSLog(@"defaults = %@", defaultsDict);
0

精彩评论

暂无评论...
验证码 换一张
取 消