如何区分通知的两个来电者?

时间:2013-01-09 12:49:19

标签: ios uitextfield uitextview

我遇到了同样的问题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];

这样从不调用的处理程序:/

有没有人知道该怎么做?

1 个答案:

答案 0 :(得分:0)

不幸的是,这些通知都有一个nil对象参数,这就是为什么你试图拥有一个对象参数不起作用的原因。您可以使用文本字段和文本视图委托方法(textFieldShouldBeginEditing或textFieldDidBeginEditing及其文本视图等效项),而不是使用这些通知。在收到键盘通知之前调用这两个委托方法。请记住使您的类成为文本字段和文本视图的委托。