gpt4 book ai didi

iOS UITextField 值无需点击 RETURN 按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:52 26 4
gpt4 key购买 nike

是否可以在不点击 RETURN 按钮的情况下获取 UITextField 值?如果我在 LOGIN UITextField 中输入内容,然后点击 PASSWORD UITextField,看起来 LOGIN 值为空,但是,如果我在登录中输入一些内容,然后点击返回,一切都很好。

不点击返回 http://gyazo.com/2ca0f263275fd65ae674233f34d90280

点击返回 http://gyazo.com/9ccc39ba7080b6b6344454ec757d3c0f

这是我的代码:

TextInputTableViewCell.m

@implementation TextInputTableViewCell

-(void)configureWithDictionary:(NSMutableDictionary *)dictionary
{
self.cellInfoDictionary = dictionary;

NSString *title = [dictionary objectForKey:@"title"];
NSString *imageName = [dictionary objectForKey:@"imageName"];
UIColor *color = [dictionary objectForKey:@"bgColor"];
BOOL secureTextEntry = [dictionary objectForKey:@"secure"];

self.myTextField.placeholder = title;
self.myImageView.image = [UIImage imageNamed:imageName];
self.contentView.backgroundColor = color;
self.myTextField.secureTextEntry = secureTextEntry;

self.myTextField.delegate = self;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];

if (textField.text)
{
[self.cellInfoDictionary setObject:textField.text
forKey:@"value"];
}

return NO;
}

@end

LoginViewController.m

@interface LoginViewController () <UITableViewDataSource, UITableViewDelegate, NewRestHandlerDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *datasource;

@end

static NSString *textInputCellIdentifier = @"textInputCellIdentifier";
static NSString *buttonCellIdentifier = @"buttonCellIdentifier";


@implementation LoginViewController
{
NSString *email;
NSString *password;
NewRestHandler *restHandler;
}

- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"logged"])
[self performSegueWithIdentifier:@"Logged"
sender:self];
[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TextInputTableViewCell class])
bundle:nil]
forCellReuseIdentifier:textInputCellIdentifier];

[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ButtonTableViewCell class])
bundle:nil]
forCellReuseIdentifier:buttonCellIdentifier];

restHandler = [[NewRestHandler alloc] init];
restHandler.delegate = self;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *dictionary = [self.datasource objectAtIndex:indexPath.row];
NSString *cellIdentifier = [dictionary objectForKey:@"cellIdentifier"];

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];

if ([cell respondsToSelector:@selector(configureWithDictionary:)])
{
[cell performSelector:@selector(configureWithDictionary:)
withObject:dictionary];
}

return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.datasource.count;
}

...

- (NSArray *)datasource
{
if (!_datasource)
{
NSMutableArray* datasource = [NSMutableArray arrayWithCapacity:5];

NSMutableDictionary* loginDictionary = @{@"cellIdentifier": textInputCellIdentifier,
@"title": @"Login",
@"imageName": @"edycja02.png",
@"bgColor": [UIColor whiteColor],
}.mutableCopy;

NSMutableDictionary* passwordDictionary = @{@"cellIdentifier": textInputCellIdentifier,
@"title": @"Password",
@"imageName": @"edycja03.png",
@"bgColor": [UIColor whiteColor],
@"secure": @YES,
}.mutableCopy;

NSMutableDictionary* loginButtonDictionary = @{@"cellIdentifier": buttonCellIdentifier,
@"title": @"Login",
@"imageName": @"logowanie01.png",
@"bgColor": [UIColor colorWithRed:88/255.0 green:88/255.0 blue:90/255.0 alpha:1],
@"textColor": [UIColor whiteColor],
}.mutableCopy;

NSMutableDictionary* facebookLoginButtonDictionary = @{@"cellIdentifier": buttonCellIdentifier,
@"title": @"Login with Facebook",
@"imageName": @"logowanie02.png",
@"bgColor": [UIColor colorWithRed:145/255.0 green:157/255.0 blue:190/255.0 alpha:1],
@"textColor": [UIColor whiteColor],
}.mutableCopy;

NSMutableDictionary* signUpButtonDictionary = @{@"cellIdentifier": buttonCellIdentifier,
@"title": @"Sign up",
@"imageName": @"logowanie03.png",
@"bgColor": [UIColor colorWithRed:209/255.0 green:210/255.0 blue:212/255.0 alpha:1],
@"textColor": [UIColor whiteColor],
}.mutableCopy;

[datasource addObject:loginDictionary];
[datasource addObject:passwordDictionary];
[datasource addObject:loginButtonDictionary];
[datasource addObject:facebookLoginButtonDictionary];
[datasource addObject:signUpButtonDictionary];

_datasource = datasource;
}
return _datasource;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 2)
{
email = [self.datasource[0] valueForKey:@"value"];
password = [self.datasource[1] valueForKey:@"value"];

NSLog(@"Email: %@", email);
NSLog(@"Password: %@", password);

...

最佳答案

你可以简单地使用这个...

 -(void)textFieldBeginEditing:(UITextField *)textField
{
if(textfield == Password)
{
if(login.text.length isEqualtoString:@"")
{
//Alert show that login text should not be empty or do your code
}
}
}

关于iOS UITextField 值无需点击 RETURN 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27520469/

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