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

转载于:https://www.cnblogs.com/hcxl/p/8321571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值