inputAccessoryView Не вызывается при появлении клавиатуры

Я добавил InputAccessoryView, чтобы моя клавиатура исчезла. Работает нормально. Но в некоторых ViewController его нет. Даже этот метод не вызывает.

У меня есть кнопка. Когда я нажимаю на это, мое текстовое поле автоматически становится первым ответчиком. (курсор фокусируется на текстовом поле при нажатии на эту кнопку) Клавиатура появляется, но вид входного аксессуара не появляется.

Я тестирую это на устройстве iOS 8.2. это мой код

in viewDidload

 // for search------------------
UIView *srchPadView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
[srchtextbox setLeftView:srchPadView];
[srchtextbox setLeftViewMode:UITextFieldViewModeAlways];
srchtextbox.attributedPlaceholder=[[NSAttributedString alloc] initWithString:alertStrings.strSearch attributes:@{NSForegroundColorAttributeName: [UIColor colorWithRed:1 green:1 blue:1 alpha:0.6]}];
[srchtextbox setTextColor:[UIColor whiteColor]];
srchtextbox.delegate=self;

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(textFieldTextDidChangeOneCI:)
 name:UITextFieldTextDidChangeNotification
 object:srchtextbox];

в методе изменения текстового поля

-(void)textFieldTextDidChangeOneCI:(NSNotification *)notification
 {

    isSearching=YES;
    [NSObject cancelPreviousPerformRequestsWithTarget:self
                                         selector:@selector(getSongs)
                                           object:nil];

    loading=YES;
    [mainactivityindicator startAnimating];
    songsArray=[[NSMutableArray alloc]init];
    songstable.hidden=YES;
    startRec=0;

    [self performSelectorInBackground:@selector(getSongs) withObject:nil];


}

потом

- (UIView *)inputAccessoryView {
if (!inputAccessoryView) {
    CGRect accessFrame = CGRectMake(0.0, 0.0, 320, 40);
    inputAccessoryView = [[UIView alloc] initWithFrame:accessFrame];
    inputAccessoryView.backgroundColor = [UIColor colorWithRed:70.0/255 green:70.0/255.0 blue:70.0/255.0 alpha:0.9];

    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 1)];
    [line setBackgroundColor:[UIColor colorWithRed:134.0/255.0 green:139.0/255.0 blue:143.0/255.0 alpha:1.0]];
    [inputAccessoryView addSubview:line];

    UIButton *compButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    compButton.frame = CGRectMake(260.0, 2, 60, 37.0);
    [compButton.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [compButton setTitle: @"Done" forState:UIControlStateNormal];
    [compButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [compButton addTarget:self action:@selector(completeCurrentWord:)
         forControlEvents:UIControlEventTouchUpInside];
    [inputAccessoryView addSubview:compButton];



}
    return inputAccessoryView;
}

Ну наконец то

-(IBAction)completeCurrentWord:(id)sender{
[self.view endEditing:YES];

}

Но мой inputAccessoryView никогда не звонит. Почему это. Как я могу решить эту проблему? Пожалуйста помогите.

Спасибо


person IRD    schedule 11.05.2015    source источник
comment
где вы называете свою функцию - (UIView *)inputAccessoryView?   -  person Vijay Masiwal    schedule 11.05.2015


Ответы (1)


Тебе нужно

  1. переопределите метод inputAccessoryView и верните свое представление.
  2. метод переопределения:
    -(BOOL)canBecomeFirstResponder{
        return YES;
    }

3.вызовите [self becomeFirstResponder];, когда вам нужно, чтобы ваш входной аксессуар был виден

Подсказка: если вы хотите, чтобы вас всегда показывали

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}
person Henawey    schedule 15.12.2015
comment
Кроме того, вызовите [self resignFirstResponder]; в viewWillDisappear:, чтобы оживленно отклонить его :) - person Roger Oba; 15.04.2017