从UITextField获取输入

时间:2014-03-23 20:49:29

标签: ios objective-c uitextfield

我正在为我的iOS应用程序做一个程序化的UI,我有两个UITextField。当客户端打开应用程序时,他们会选择两个字段并提供一些输入,之后,如何取消选择UITextField?因为如果您点击或滚动屏幕上的其他位置,键盘会停留在屏幕上 - 它不会像预期的行为一样缩回。

所以我想我的问题是如何在没有UITextFieldnib的情况下提交storyboard

以下是viewDidLoad的相关部分,其中包含两个UITextField

UITextField *loginField = [[UITextField alloc] initWithFrame:mistarFrame];
loginField.backgroundColor = [UIColor clearColor];
loginField.textColor = [UIColor whiteColor];
loginField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
loginField.borderStyle = UITextBorderStyleNone;
loginField.autocorrectionType = UITextAutocorrectionTypeNo;
loginField.autocapitalizationType = UITextAutocapitalizationTypeNone;
loginField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
loginField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Student ID" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
[header addSubview:loginField];

UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(mistarFrame.origin.x, (mistarFrame.origin.y + iconHeight), mistarFrame.size.width, mistarFrame.size.height)];
passwordField.backgroundColor = [UIColor clearColor];
passwordField.textColor = [UIColor whiteColor];
passwordField.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:20];
passwordField.borderStyle = UITextBorderStyleNone;
passwordField.autocorrectionType = UITextAutocorrectionTypeNo;
passwordField.autocapitalizationType = UITextAutocapitalizationTypeNone;
passwordField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
passwordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Password" attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
passwordField.secureTextEntry = YES;
[header addSubview:passwordField];

3 个答案:

答案 0 :(得分:2)

此外,您可以符合UITextFieldDelegate协议并设置

loginField.delegate = self;
passwordField.delegate = self;

然后实施

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if ([loginField.text isEqualToString:@""] == NO && [passwordField.text isEqualToString:@""] == NO) {
        //submit...
    }
    return YES;
}

如果您想通过按键盘上的返回提交,那就是

这是一个示例实现文件......

#import "ViewController.h"

@interface ViewController () <UITextFieldDelegate>

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 30, 100, 25)];
    [self.view addSubview:tf];
    tf.delegate = self;
}

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if ([textField.text isEqualToString:@""] == NO) {
        NSLog(@"Submit");
    }
    return YES;
 }

@end

答案 1 :(得分:1)

我建议在您的UITextViews所在的视图中添加UIGestureRecognizer,然后连接&#34;隐藏键盘事件&#34;点击动作。

例如,如果UITextViews位于UIViewController的视图中:

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *screenTapped = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                               action:@selector(dismissKeyboard)];
    screenTapped.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:screenTapped];
}

- (void)dismissKeyboard {
    [self.view endEditing:YES];
}

答案 2 :(得分:0)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [loginField resignFirstResponder];
    //..
}

试试这个。