ios 如何在键盘上添加完成按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10077155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add Done button to the keyboard?
提问by Richard Knop
UPDATE:
更新:
I also tried implementing UITextViewDelegate delegate and then doing in my controller:
我还尝试实现 UITextViewDelegate 委托,然后在我的控制器中执行:
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
[textView resignFirstResponder];
return YES;
}
I also set the delegate of the text view to be self (controller view instance).
我还将文本视图的委托设置为 self(控制器视图实例)。
Clicking the Done button still inserts just a new line :(
单击完成按钮仍然只插入一个新行:(
UPDATE:
更新:
What I have done so far. I have implemented a UITextFieldDelegate by my view controller.
到目前为止我所做的。我已经通过我的视图控制器实现了一个 UITextFieldDelegate。
I have connected the text view to the view controller via outlet.
我已经通过插座将文本视图连接到视图控制器。
Then I did:
然后我做了:
self.myTextView.delegate = self;
And:
和:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
But when I click on Done button, it just adds a new line.
但是当我点击完成按钮时,它只是添加了一个新行。
So I have a UITextView element on my scene and when a user taps it, keyboard appears and it can be edited.
所以我的场景中有一个 UITextView 元素,当用户点击它时,键盘会出现并且可以进行编辑。
However, I cannot dismiss the keyboard.
但是,我不能关闭键盘。
How can I add Done button to the keyboard so it can be dismissed?
如何将“完成”按钮添加到键盘以便可以将其关闭?
回答by Sebastian Flückiger
thats quite simple :)
那很简单:)
[textField setReturnKeyType:UIReturnKeyDone];
for dismissing the keyboard implement the <UITextFieldDelegate>
protocol in your class, set
要关闭键盘<UITextFieldDelegate>
,请在您的课程中实现协议,设置
textfield.delegate = self;
and use
并使用
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}
or
或者
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
回答by Mike Z
Go into your storyboard, select your text field and under the Attributes Inspector there is an option that says "return key"...select "Done".
进入你的故事板,选择你的文本字段,在属性检查器下有一个选项,上面写着“返回键”...选择“完成”。
Then go into your ViewController and add:
然后进入你的 ViewController 并添加:
- (IBAction)dismissKeyboard:(id)sender;
{
[textField becomeFirstResponder];
[textField resignFirstResponder];
}
Then go back to your text field, click outlets and link "Did end on exit" to dismissKeyboard action.
然后返回到您的文本字段,单击出口并链接“退出时结束”以关闭键盘操作。
回答by ViJay Avhad
Add UIToolBar as custom view that will have Done button as UIBarButtonItem in it.
添加 UIToolBar 作为自定义视图,其中将有完成按钮作为 UIBarButtonItem。
This is safer and cleaner way to add Done button to any Type of Keyboard. Create UIToolBar add Done Button to it and set inputAccessoryView of any UITextField or UITextView.
这是将“完成”按钮添加到任何类型的键盘的更安全、更简洁的方法。创建UIToolBar添加完成按钮,它和任何的UITextField或UITextView的集inputAccessoryView。
UIToolbar *ViewForDoneButtonOnKeyboard = [[UIToolbar alloc] init];
[ViewForDoneButtonOnKeyboard sizeToFit];
UIBarButtonItem *btnDoneOnKeyboard = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(doneBtnFromKeyboardClicked:)];
[ViewForDoneButtonOnKeyboard setItems:[NSArray arrayWithObjects:btnDoneOnKeyboard, nil]];
myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard;
IBAction For Done Button
完成按钮的 IBAction
- (IBAction)doneBtnFromKeyboardClicked:(id)sender
{
NSLog(@"Done Button Clicked.");
//Hide Keyboard by endEditing or Anything you want.
[self.view endEditing:YES];
}
SWIFT 3
斯威夫特 3
var ViewForDoneButtonOnKeyboard = UIToolbar()
ViewForDoneButtonOnKeyboard.sizeToFit()
var btnDoneOnKeyboard = UIBarButtonItem(title: "Done", style: .bordered, target: self, action: #selector(self.doneBtnFromKeyboardClicked))
ViewForDoneButtonOnKeyboard.items = [btnDoneOnKeyboard]
myTextField.inputAccessoryView = ViewForDoneButtonOnKeyboard
Function
功能
@IBAction func doneBtnFromKeyboardClicked (sender: Any) {
print("Done Button Clicked.")
//Hide Keyboard by endEditing or Anything you want.
self.view.endEditing(true)
}
回答by Esqarrouth
Swift version:
迅捷版:
In Your ViewController:
在您的 ViewController 中:
textField.returnKeyType = UIReturnKeyType.Done
textField.delegate = self
After your ViewController
在您的 ViewController 之后
extension MyViewController: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
回答by Garreth Golding
Set the 'Return Key'
option under the text field
option in the inspector for your text field.
Then right click on the text field and while holding CTRL
select 'Did End On Exit'
and drag this into your view controllers
file. This will create an IBAction
method. Give the method a name and then enter the following values:
在文本字段的检查器中的'Return Key'
选项下设置选项text field
。然后右键单击文本字段并按住CTRL
选择'Did End On Exit'
并将其拖到您的view controllers
文件中。这将创建一个IBAction
方法。为方法命名,然后输入以下值:
[textField becomeFirstResponder];
[textField resignFirstResponder];
Your method should look like this:
您的方法应如下所示:
- (IBAction)methodName:(id)sender;
{
[sender becomeFirstResponder];
[sender resignFirstResponder];
}
回答by Donato Perconti
Ok, so I too have been struggling with this very issue. Currently I am accessing a UITextView in a UITableViewCell (via Tags). Because I am using prototype cells I cannot use IBActions nor IBOutlets like everyone suggests. Instead, I am using;
好的,所以我也一直在努力解决这个问题。目前我正在访问 UITableViewCell 中的 UITextView(通过标签)。因为我使用原型单元格,所以我不能像大家建议的那样使用 IBActions 或 IBOutlets。相反,我正在使用;
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
This will then provide me with the text each time the user hits a button. My solution then, was to get the ascii character for new line; "/n". If that is the text that was entered, I would resign the first responder. For example;
每次用户点击按钮时,这都会为我提供文本。我的解决方案是获取新行的 ascii 字符;“/n”。如果这是输入的文本,我会辞去第一响应者的职务。例如;
// If the text length is 0 then the user is deleting something, so only check the ascii character if there is text to check if (text.length != 0) { // Get the Ascii character int asciiCode = [text characterAtIndex:0]; // If the ascii code is /n or new line, then resign first responder if (asciiCode == 10) { [alertTextView resignFirstResponder]; [DeviceTextView resignFirstResponder]; } }
// If the text length is 0 then the user is deleting something, so only check the ascii character if there is text to check if (text.length != 0) { // Get the Ascii character int asciiCode = [text characterAtIndex:0]; // If the ascii code is /n or new line, then resign first responder if (asciiCode == 10) { [alertTextView resignFirstResponder]; [DeviceTextView resignFirstResponder]; } }
Not sure if anyone else will need this hacky solution but I figured I'd put it out there in case someone needs it!
不确定是否还有其他人需要这个 hacky 解决方案,但我想我会把它放在那里以防有人需要它!
回答by DURGESH
This is best way to add Done Button on keyboard
这是在键盘上添加完成按钮的最佳方式
textField.returnKeyType = UIReturnKeyDone;
回答by AechoLiu
The following is my approach, in Swift 3
. When the doneBtn
clicked, let it send .editingDidEndOnExit
event. I use this event to handle focus problem between multiple textFields.
以下是我的方法,在Swift 3
. 当doneBtn
点击,让它发送.editingDidEndOnExit
事件。我使用这个事件来处理多个 textFields 之间的焦点问题。
// My customized UITextField
class MyTextField: UITextField {
override func awakeFromNib() {
super.awakeFromNib()
// Add toolBar when keyboardType in this set.
let set : [UIKeyboardType] = [.numberPad, .phonePad]
if (set.contains(self.keyboardType) ) {
self.addDoneToolbar()
}
}
// Add a toolbar with a `Done` button
func addDoneToolbar() {
let toolbar = UIToolbar()
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
let doneBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(onToolBarDone))
toolbar.items = [space, doneBtn]
toolbar.sizeToFit()
self.inputAccessoryView = toolbar
}
@objc func onToolBarDone() {
// I use `editingDidEndOnExit` to simulate the `Done` behavior
// on the original keyboard.
self.sendActions(for: .editingDidEndOnExit)
}
}
回答by Ryan Poolos
In Interface Builder on the properties of the textView you can set the return button type to Done.
在界面生成器的 textView 属性上,您可以将返回按钮类型设置为完成。
Then you need to check for when the Return button is pressed using
然后您需要检查何时按下返回按钮
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
Check if the text == "/n" then dismiss the keyboard by resigning first responder on your textView.
检查 text == "/n" 然后通过在 textView 上退出第一响应者来关闭键盘。