我有一个buttonA,当buttonA被点击时,我希望键盘在inputAccessoryView中显示一个UITextField.有没有办法手动显示键盘,还要设置inputAccessoryView最初没有UITextField?
谢谢.
解决方法
您无法召唤键盘,而无需成为第一反应者的对象.有两种方法可以解决:
>将UIView子类化并在其中实现UIKeyInput协议.例如:
在.h文件中:
@interface InputObject : UIView<UIKeyInput> @property (nonatomic,copy) Nsstring *text; @property (nonatomic,strong) UIView *inputAccessoryView; // You must override inputAccessoryView,since it's readonly by default @end
在.m文件中,实现协议:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
- (BOOL)hasText
{
return [self.text length];
}
- (void)insertText:(Nsstring *)text
{
Nsstring *selfText = self.text ? self.text : @"";
self.text = [selfText stringByAppendingString:text];
[[NSNotificationCenter defaultCenter] postNotificationName:kInputObjectTextDidChangeNotification object:self];
}
- (void)deleteBackward
{
if ([self.text length] > 0)
self.text = [self.text substringToIndex:([self.text length] - 1)];
else
self.text = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:kInputObjectTextDidChangeNotification object:self];
}
假设你想要在你的-viewDidAppear中召唤键盘,代码如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
// inputObject and textField are both your ivars in your view controller
inputObject = [[InputObject alloc] initWithFrame:CGRectMake(0,100,100)];
textField = [[UITextField alloc] initWithFrame:CGRectMake(0,30)];
inputObject.inputAccessoryView = textField;
[self.view addSubview:inputObject];
[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(inputObjectTextDidChange:) name:kInputObjectTextDidChangeNotification object:nil];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[inputObject becomeFirstResponder]; // This will summon the keyboard
}
然后在视图控制器中实现通知选择器:
- (void)inputObjectTextDidChange:(NSNotification *)notification
{
// Just set the text here. notification.object is actually your inputObject.
textField.text = ((InputObject *)(notification.object)).text;
}
这可能是你的意思是“设置inputAccessoryView最初没有UITextField”
>另一个解决方法是让textField“假装”通过仔细安排其动画来成为inputAccessoryView.但是这个解决方案需要你的textField成为第一个响应者.
首先,您观察到您的-viewDidLoad中的键盘事件:
- (void)viewDidLoad
{
[super viewDidLoad];
// Init the textField and add it as a subview of self.view
textField = [[UITextField alloc] init];
textField.backgroundColor = [UIColor redColor];
[self.view addSubview:textField];
// Register keyboard events
[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
其次,在-viewWillAppear:中设置textField的框架,以保证其框架不会受到自动调整的影响:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
textField.frame = CGRectMake(0,CGRectGetMaxY(self.view.bounds),CGRectGetWidth(self.view.bounds),50);
}
然后,安排您的textField动画,让它与键盘动画同步.您的键盘通知选择器可能如下所示:
- (void)keyboardWillShowNotification:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
UIViewAnimationCurve curve = [[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat duration = [[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect keyboardFrame = [[userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame toView:self.view];
[UIView animateWithDuration:duration delay:0.0f options:(UIViewAnimationoptions)curve animations:^{
CGRect textFieldFrame = textField.frame;
textFieldFrame.origin.y = keyboardFrame.origin.y - CGRectGetHeight(textFieldFrame);
textField.frame = textFieldFrame;
}completion:nil];
}
- (void)keyboardWillHideNotification:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
UIViewAnimationCurve curve = [[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGFloat duration = [[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration delay:0.0f options:(UIViewAnimationoptions)curve animations:^{
textField.frame = CGRectMake(0,50);
}completion:nil];
}
最后,调用[textField成为FirstResponder]来触发动画.