iOS键盘点击enter键发生的那些事儿

原创
2015/10/09 10:41
阅读数 5.6K
#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
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部