ฉันมีสองUITextFieldsชื่อ (เช่นชื่อผู้ใช้และรหัสผ่าน) แต่ฉันไม่สามารถกำจัดแป้นพิมพ์ได้เมื่อกดปุ่มย้อนกลับบนแป้นพิมพ์ ฉันจะทำเช่นนี้ได้อย่างไร?
ฉันมีสองUITextFieldsชื่อ (เช่นชื่อผู้ใช้และรหัสผ่าน) แต่ฉันไม่สามารถกำจัดแป้นพิมพ์ได้เมื่อกดปุ่มย้อนกลับบนแป้นพิมพ์ ฉันจะทำเช่นนี้ได้อย่างไร?
คำตอบ:
ก่อนอื่นคุณต้องปฏิบัติตามUITextFieldDelegateโปรโตคอลในไฟล์ส่วนหัวของ View / ViewController ดังนี้:
@interface YourViewController : UIViewController <UITextFieldDelegate>จากนั้นในไฟล์. m ของคุณคุณต้องใช้UITextFieldDelegateวิธีโปรโตคอลต่อไปนี้:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}[textField resignFirstResponder]; ตรวจสอบให้แน่ใจว่าคีย์บอร์ดถูกปิด
ตรวจสอบให้แน่ใจว่าคุณตั้งค่ามุมมอง / ตัวควบคุมมุมมองของคุณเป็นผู้รับมอบสิทธิ์ของ UITextField หลังจากที่คุณเริ่มต้นฟิลด์ข้อความใน. m:
yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....    
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is onใช้วิธี UITextFieldDelegate ดังนี้:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
    [aTextField resignFirstResponder];
    return YES;
}โปรดดูการจัดการคีย์บอร์ดสำหรับการสนทนาทั้งหมดในหัวข้อนี้
UITextFields ของคุณควรมีวัตถุที่ได้รับมอบหมาย (UITextFieldDelegate) ใช้รหัสต่อไปนี้ในผู้รับมอบสิทธิ์ของคุณเพื่อทำให้แป้นพิมพ์หายไป:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
}น่าทำงานจัง ...
เอาการทดลองสองสามครั้งมาให้ฉันมีปัญหาเดียวกันสิ่งนี้ใช้ได้กับฉัน:
ตรวจสอบการสะกดของคุณได้ที่ -
(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];ฉันแก้ไขของฉันtextFieldแทนtextfieldใช้ตัวพิมพ์ใหญ่ "F" ... และบิงโก !! มันได้ผล ..
เมื่อกดปุ่มย้อนกลับให้โทร:
[uitextfield resignFirstResponder];หลังจากใช้เวลาพอสมควรในการค้นหาสิ่งที่เข้าท่านี่คือสิ่งที่ฉันรวบรวมและมันได้ผลอย่างมีเสน่ห์
.h
//
//  ViewController.h
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;
@end.m
//
//  ViewController.m
//  demoKeyboardScrolling
//
//  Created by Chris Cantley on 11/14/13.
//  Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    // _theTextField is the name of the parameter designated in the .h file. 
    _theTextField.returnKeyType = UIReturnKeyDone;
    [_theTextField setDelegate:self];
}
// This part is more dynamic as it closes the keyboard regardless of what text field 
// is being used when pressing return.  
// You might want to control every single text field separately but that isn't 
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
}
@endหวังว่านี่จะช่วยได้!
ตั้งค่า Delegate ของ UITextField ให้กับ ViewController ของคุณเพิ่มเต้าเสียบอ้างอิงระหว่าง File's Owner และ UITextField จากนั้นใช้วิธีนี้:
-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{
   if (textField == yourTextField) 
   {
      [textField resignFirstResponder]; 
   }
   return NO;
}เพิ่มสิ่งนี้แทนคลาสที่กำหนดไว้ล่วงหน้า
class ViewController: UIViewController, UITextFieldDelegate {เพื่อลบแป้นพิมพ์เมื่อคลิกนอกแป้นพิมพ์
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }และเพื่อลบแป้นพิมพ์เมื่อกด Enter
เพิ่มบรรทัดนี้ใน viewDidLoad ()
inputField คือชื่อของ textField ที่ใช้
self.inputField.delegate = selfและเพิ่มฟังก์ชันนี้
func textFieldShouldReturn(textField: UITextField) -> Bool {        
        textField.resignFirstResponder()        
        return true        
    }Swift 2:
นี่คือสิ่งที่ทำได้ทุกสิ่ง!
แป้นพิมพ์ใกล้ชิดกับDoneปุ่มหรือการTouch outSide,Nextสำหรับการเดินทางเพื่อป้อนต่อไป
เปลี่ยน TextFiled Return KeyการNextในสตอรี่บอร์ด
override func viewDidLoad() {
  txtBillIdentifier.delegate = self
  txtBillIdentifier.tag = 1
  txtPayIdentifier.delegate  = self
  txtPayIdentifier.tag  = 2
  let tap = UITapGestureRecognizer(target: self, action: "onTouchGesture")
  self.view.addGestureRecognizer(tap)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
   if(textField.returnKeyType == UIReturnKeyType.Default) {
       if let next = textField.superview?.viewWithTag(textField.tag+1) as? UITextField {
           next.becomeFirstResponder()
           return false
       }
   }
   textField.resignFirstResponder()
   return false
}
func onTouchGesture(){
    self.view.endEditing(true)
}อย่างรวดเร็วคุณควรมอบหมาย UITextfieldDelegateสิ่งสำคัญอย่าลืมมันใน viewController เช่น:
class MyViewController: UITextfieldDelegate{
     mytextfield.delegate = self
     func textFieldShouldReturn(textField: UITextField) -> Bool {
          textField.resignFirstResponder()
     }
}คุณสามารถเพิ่ม IBAction ลงใน uiTextField (เหตุการณ์ที่เกี่ยวข้องคือ "Did End On Exit") และ IBAction อาจตั้งชื่อว่า hideKeyboard
-(IBAction)hideKeyboard:(id)sender
{
    [uitextfield resignFirstResponder];
}นอกจากนี้คุณสามารถนำไปใช้กับช่องข้อความหรือปุ่มอื่น ๆ ได้เช่นคุณอาจเพิ่มปุ่มที่ซ่อนอยู่ในมุมมองเมื่อคุณคลิกเพื่อซ่อนแป้นพิมพ์
คุณสามารถลองคลาสย่อย UITextfield ซึ่งคุณสามารถตั้งเงื่อนไขให้ข้อความเปลี่ยน UIReturnKey แบบไดนามิก: 
https://github.com/codeinteractiveapps/OBReturnKeyTextField
หากคุณต้องการหายแป้นพิมพ์เมื่อเขียนบนไฟล์ข้อความกล่องการแจ้งเตือน
[[alertController.textFields objectAtIndex:1] resignFirstResponder];