我在这里描述了同样的问题How to add text field to inputAccessoryView and make the textView first responder和 Tom 发布的解决方案运行良好。现在我的新问题是 UITextField 和 UITextView 使用相同的 inputAccessoryView 并且还没有找到区分哪个是调用者的方法。
这是一个小代码:
@property (unsafe_unretained, nonatomic) IBOutlet UITextField *titleField;
@property (unsafe_unretained, nonatomic) IBOutlet UITextView *reviewField;
在 viewDidLoad 中:
- (void)viewDidLoad
{
[super viewDidLoad];
self.titleField.inputAccessoryView = self.accessorView;
self.reviewField.inputAccessoryView = self.accessorView;
self.accessorView.delegate = self;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeFirstResponder:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fillAccessorView:)
name:UIKeyboardWillShowNotification
object:nil];
}
和两个处理程序
- (void) fillAccessorView:(NSNotification *) notification
{
self.accessorView.titleLabel.text = @"MY Title";
}
- (void) changeFirstResponder:(NSNotification *) notification
{
[self.accessorView.textInputField becomeFirstResponder];
}
我已经尝试过这样
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeFirstResponder:)
name:UIKeyboardDidShowNotification
object:self.titleField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fillAccessorView:)
name:UIKeyboardWillShowNotification
object:self.titleField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeFirstResponder:)
name:UIKeyboardDidShowNotification
object:self.reviewField];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fillAccessorView:)
name:UIKeyboardWillShowNotification
object:self.reviewField];
这样处理程序从未调用过:/
有谁知道该怎么做?