2015-05-11 46 views
0

我已將InputAccessoryView添加到我的鍵盤消失。它很好。但在一些ViewController它沒有出現。即使這種方法不打電話。inputAccessoryView當鍵盤出現時不會調用

我有一個按鈕。當我點擊我的文本字段自動成爲第一響應者。 (點擊該按鈕時光標將重點放在textField上)鍵盤即將到來,但輸入附件視圖不會到來。

我在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打電話。這是爲什麼。我怎麼解決這個問題?請幫幫我。

感謝

+0

,你打電話給你的功能' - (UIView *)inputAccessoryView'? –

回答

1

你需要 1-覆蓋方法inputAccessoryView並返回你的看法。
2-

-(BOOL)canBecomeFirstResponder{ 
    return YES; 
} 

3調用[self becomeFirstResponder];當您需要輸入附件視圖可見

提示: 如果你想始終顯示

-(void)viewDidAppear:(BOOL)animated{ 
    [super viewDidAppear:animated]; 
    [self becomeFirstResponder]; 
} 
+1

此外,請在'viewWillDisappear:'中調用'[self resignFirstResponder];'以便動態地消除它:) –