iOS鍵盤點擊enter鍵發生的那些事兒

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *usernameTextfield;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextfield;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //設置usernameTextfield及passwordTextfield的相關屬性
    
    //設置 usernameTextfield 的鍵盤類型
    /*
     typedef NS_ENUM(NSInteger, UIKeyboardType) {
     UIKeyboardTypeDefault,                // Default type for the current input method.
     UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
     UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
     UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
     UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
     UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
     UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
     UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
     UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
     UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
     UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
     
     UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated
     
     };
     */
    
    //self.usernameTextfield.keyboardType = UIKeyboardTypeNumberPad;
    
    //設置usernameTextfield的 returnkey
    self.usernameTextfield.returnKeyType = UIReturnKeyNext;
    
    //設置代理前必須讓本類支持該協議,並將代理設置爲本身
    self.usernameTextfield.delegate = self;
    
    
    //設置 usernameTextfield
    self.passwordTextfield.keyboardType = UIKeyboardTypeEmailAddress;
    self.passwordTextfield.returnKeyType = UIReturnKeyDone;
    self.passwordTextfield.delegate =self;
    
    //使你輸入的密碼變爲小原點,及通常應用都這麼作
    self.passwordTextfield.secureTextEntry = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)clickLoginBtn:(UIButton *)sender {
    NSLog(@"登陸成功");
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField == self.usernameTextfield) {
        //self.usernameTextfield放棄第一響應者,而self.passwordTextfield變爲第一響應者
        [self.usernameTextfield resignFirstResponder];
        [self.passwordTextfield becomeFirstResponder];
    } else if(textField == self.passwordTextfield) {
        //self.passwordTextfield放棄第一響應者,並調用登陸函數
        [self.passwordTextfield resignFirstResponder];
        [self.loginBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
        //[self clickLoginBtn:self.loginBtn];
    }
    return YES;
}
@end
相關文章
相關標籤/搜索