iOS 中 textFieldShouldReturn 方法的使用

iOS 中 textFieldShouldReturn 方法的使用

Aug 31, 2014
Objc, Xcode

iOS 中 textFieldShouldReturn 方法定义在 UITextFieldDelegate 协议中

//  ViewController.m


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField==_name) {
        [_passwd becomeFirstResponder];
    }else if(textField==_passwd){
        [self login:nil];
    }
    return YES;
}

- (IBAction)login:(id)sender {
    [self.view endEditing:NO];
    NSString *name = _name.text;
    NSString *passwd = _passwd.text;
    [_lable setText:[NSString stringWithFormat:@"username:%@ password:%@",name,passwd]];
    }
@end

img

本文共 91 字,上次修改于 Jan 28, 2024,以 CC 署名-非商业性使用-禁止演绎 4.0 国际 协议进行许可。