我的应用程序的一个目标是管理多个自定义输入视图(来自笔尖)以及常规系统键盘。一个或另一个将永远存在。 (在示例代码中,我只管理单个笔尖+系统键盘)。
换掉键盘时,我想保留使用系统键盘看到的滑入/滑出效果。为了获得自定义键盘和系统键盘的滑动动画,我从文本字段中取出resignFirstResponder并等到键盘被隐藏。然后我执行一个becomeFirstResponder来引入新的键盘。我使用UIKeyboardDidHideNotification作为触发器来触发becomeFirstResponder并加载新键盘。
我看到的问题是UIKeyboardDidHideNotification在执行resignFirstResponder(正如预期)时再次触发两次......再次执行becomeFirstResponder时触发。我怀疑我可以放入一些状态来检测第二个通知,但我想了解一下,为什么第一个反应器会导致它首先触发?
#import "DemoViewController.h"
@interface DemoViewController ()
@property (strong, nonatomic) IBOutlet UITextField *dummyTextField;
@end
@implementation DemoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[[UITextField appearance] setKeyboardAppearance:UIKeyboardAppearanceDark];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self showNormalKeyboard];
[_dummyTextField becomeFirstResponder];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)keyboardDidHide:(NSNotification*) notification {
NSLog(@"Keyboard hidden: %@", notification);
// Executing this next line causes the notification to fire again
[_dummyTextField becomeFirstResponder];
}
-(void)showAlternateKeyboard{
_dummyTextField.inputView = [[[NSBundle mainBundle] loadNibNamed:@"AlternateKeyboardView" owner:self options:nil] lastObject];
[_dummyTextField resignFirstResponder];
}
-(void)showNormalKeyboard{
_dummyTextField.inputView = nil;
[_dummyTextField resignFirstResponder];
}
// This is a button on the DemoViewController screen
// that is always present.
- (IBAction)mainButtonPressed:(UIButton *)sender {
NSLog(@"Main Screen Button Pressed!");
}
// This is a button from the Custom Input View xib
// but hardwired directly into here
- (IBAction)alternateKeyboardButtonPressed:(UIButton *)sender {
NSLog(@"Alternate Keyboard Button Pressed!");
}
// This is a UISwitch on the DemoViewController storyboard
// that selects between the custom keyboard and the regular
// system keyboard
- (IBAction)keyboardSelectChanged:(UISwitch *)sender {
if (sender.on){
[self showAlternateKeyboard];
} else {
[self showNormalKeyboard];
}
}
@end
答案 0 :(得分:2)
我的猜测是你在前一个字段完全辞去第一个响应者之前正在调用becomeFirstResponder
。只是为了看看它是否有帮助,尝试添加一些延迟性能:
-(void)keyboardDidHide:(NSNotification*) notification {
NSLog(@"Keyboard hidden: %@", notification);
dispatch_async(dispatch_get_main_queue(), ^{
[_dummyTextField becomeFirstResponder];
};
}
答案 1 :(得分:0)
@interface MainViewController : UIViewController
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (nonatomic, retain) IBOutlet UISwitch *keyboardSwitch;
@property (nonatomic, retain) IBOutlet UISwitch *textFieldSwitch;
- (IBAction)toggleKeyboardVisibility;
@end
#import "MainViewController.h"
@implementation MainViewController
@synthesize textField, keyboardSwitch, textFieldSwitch;
- (void)viewDidLoad
{
[super viewDidLoad];
}
/**
* Show keyboard as soon as view appears
*
*/
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.textField becomeFirstResponder];
}
/**
* Modify the hidden property of our text field
*
*/
- (IBAction)toggleTextFieldVisibility
{
if (self.textFieldSwitch.on)
self.textField.hidden = NO;
else
self.textField.hidden = YES;
}
/**
* Change the first responder status of the keyboard
*
*/
- (IBAction)toggleKeyboardVisibility
{
if (self.keyboardSwitch.on)
[self.textField becomeFirstResponder];
else
[self.textField resignFirstResponder];
}
@end