gpt4 book ai didi

ios - 从 UITextField 获取输入

转载 作者:行者123 更新时间:2023-11-28 20:04:23 24 4
gpt4 key购买 nike

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

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

这是 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];

最佳答案

也可以按照UITextFieldDelegate协议(protocol)进行设置

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

关于ios - 从 UITextField 获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com