ios - inputAccessoryView 出现键盘时不调用

标签 ios uitextfield uitextfielddelegate inputaccessoryview

我已添加 InputAccessoryView到我的键盘消失。它炒得很好。但在某些 ViewController它没有出现。即使那个方法也不会调用。

我有一个按钮。当我点击我的文本字段时,它会自动成为第一响应者。 (单击该按钮时光标聚焦在文本字段上)键盘来了,但输入附件 View 没有来。

我正在 iOS 8.2 设备上对此进行测试。
这是我的代码

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从不打电话。这是为什么。我怎么解决这个问题?请帮我。

谢谢

最佳答案

你需要

  • 覆盖方法inputAccessoryView并返回您的 View 。
  • 覆盖方法:
  •     -(BOOL)canBecomeFirstResponder{
            return YES;
        }
    

    3.调用[self becomeFirstResponder];当您需要输入附件 View 可见时

    暗示:
    如果你想一直显示
    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        [self becomeFirstResponder];
    }
    

    关于ios - inputAccessoryView 出现键盘时不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164233/

    相关文章:

    ios - 从 UITextField 子类捕获委托(delegate)方法而不是委托(delegate)

    ios - UITextField — 观察文本选择和光标移动期间的变化

    ios - 在多个 uitextfields 之间切换

    ios - 如何让一个常规对象类做自己的事情是 NSManagedObject 的子类?

    iphone - UITextField 在高度增加时从顶部开始文本

    objective-c - 在 UITextField 上显示过去的条目

    ios - 将目标添加到 UITableview 中的 UITextField (Swift)

    ios - Xcode 5 - CPU 和内存使用失控

    c# - 以编程方式创建按钮单点触控

    ios - 如何仅在 iOS 中获取日期而不是时间