2014-09-25 60 views
1

我知道如何顯示與UITextField鍵盤配件。顯示鍵盤上的按鈕點擊在ios

textField.inputAccessoryView = keyboardToolBar; 

但是如果我沒有一個UITextField而是一個按鈕怎麼辦?如何顯示帶有工具欄的鍵盤?

工具欄本身將包含UITextField,我將不得不以編程方式成爲第一響應者。

但是,這將如何工作?

更新

-(void)createKeyboardToolBar 
{ 
    self.keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 42)]; 
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(8, 6, 250, 30)]; 
    UIBarButtonItem *textBtn = [[UIBarButtonItem alloc] initWithCustomView:self.textField ]; 
    UIBarButtonItem *postBtn = [[UIBarButtonItem alloc]initWithTitle:@"Post" style:UIBarButtonItemStyleBordered target:self action:@selector(postComment)]; 
    [self.keyboardToolBar setItems: [NSArray arrayWithObjects:textBtn,postBtn,nil]]; 
    self.textField.inputAccessoryView = self.keyboardToolBar; 

} 

所以下面的建議,我想出了上面的代碼。但是當我點擊應該讓第一響應者self.textField的按鈕時,就會發出呼叫,但鍵盤或工具欄不會出現。 (我使用NSLog)

+0

儘快顯示工具欄第一含有文本字段,然後作爲其可視使文本字段作爲第一個響應者 – Geet 2014-09-25 06:33:47

+0

我回國的幫助之前,這場鬥爭中,但建議不工作。我將添加更新以顯示我的代碼示例。 – 2014-09-25 07:26:51

回答

0

爲你的按鈕添加動作爲buttonClicked。所以當你點擊按鈕顯示鍵盤時,使用成爲第一個響應者爲你的textfield

- (void)buttonClicked { 

[myTextField becomeFirstResponder]; 

} 
0
  1. 的Alloc的文本框,工具條在全球範圍,並把它添加到工具欄。
  2. 在按鈕動作中,爲文本字段寫入'成爲第一響應者'。

下面是解

- (IBAction)keyboardaction:(id)sender { 
    self.keyToolbar_.hidden=NO; 
    CGRect lframe = self.keyToolbar_.frame; 
    lframe.origin.y = [[UIScreen mainScreen] bounds].size.height-<keyboardheight>; 
    [self.entryTxfld_ becomeFirstResponder]; 

    [UIView animateWithDuration:0.3 
          delay:0 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^ { 
         self.keyToolbar_.frame = lframe; 
        }completion:^(BOOL finished) { 

        }]; 

} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    CGRect lframe = self.keyToolbar_.frame; 
    lframe.origin.y = [[UIScreen mainScreen] bounds].size.height; 
    [UIView animateWithDuration:0.6 
          delay:0 
         options:UIViewAnimationOptionCurveEaseInOut 
        animations:^ { 
         self.keyToolbar_.frame = lframe; 
        }completion:^(BOOL finished) { 
        }]; 
    [textField resignFirstResponder]; 

    return YES; 
} 

enter image description here

在XIB的視圖中添加工具欄上的底部和工具欄添加的textfield.For參考找出圖像。 enter image description here

按鈕的點擊
+0

PLZ也添加一些代碼 – 2014-09-25 07:04:55

+0

也許「遞歸」是不工作的原因:textField既是顯示工具欄的原因,也是顯示工具欄的效果。也許iOS中的某些東西反對這種依賴性循環。 – 2014-09-25 07:39:31