是否可以在不点击 RETURN 按钮的情况下获取UITextField值?如果我在 LOGIN UITextField 中输入内容,然后点击 PASSWORD UITextField ,看起来LOGIN值为空,但是,如果我在LOGIN中键入内容,然后点击RETURN,一切都很好。
不点击RETURN http://gyazo.com/2ca0f263275fd65ae674233f34d90280
点击RETURN 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);
...
答案 0 :(得分:2)
你可以简单地使用它......
-(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
}
}
}
答案 1 :(得分:1)
您可以使用:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
}
这些是UITextFieldDelegate
方法。但是每次用户按下一个键时,应该有一个比更改字典键更好的方法。将textField
对象设置为property
的{{1}},并转到TextInputTableViewCell
中的textField
个对象。
注意:didSelectRowAtIndexPath
会在添加上一个操作之前显示文本。如果你按下&#34; a&#34;例如,当流程进入此功能时,它shouldChangeCharactersInRange
不会拥有它。请参阅文档。
答案 2 :(得分:1)
如果您点击从登录名到密码的文本字段,请在textFieldDidBeginEditing方法中,根据您的意愿将登录文本字段中输入的文本保存到NSString或NSUserDefaults。
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textfield == passwordTextField )
{
[[NSUserDefaults standardUserDefaults]setValue: [NSString stringWithFormat:@"%@",loginTextField.text]forKey:@"LOGINCREDENTIALS"];
}
}
一旦点击密码文本字段,这将自动将您的userName字符串保存到userDefaults。
您可以从userDefaults访问存储的字符串:
[[NSUserDefaults standardUserDefaults]valueForKey:@"LOGINCREDENTIALS"];
希望这对你有用。
答案 3 :(得分:1)
如果你改变:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if (textField.text)
{
[self.cellInfoDictionary setObject:textField.text
forKey:@"value"];
}
return NO;
}
为:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
if (textField.text)
{
[self.cellInfoDictionary setObject:textField.text
forKey:@"value"];
}
return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text)
{
[self.cellInfoDictionary setObject:[textField.text stringByReplacingCharactersInRange:range withString:string]
forKey:@"value"];
}
return YES;
}
一切都应该按预期工作
答案 4 :(得分:0)
是的,有可能,你可以执行
- (void)textFieldDidEndEditing:(UITextField *)textField
{
`if (textField == your textfield here)
{
if ([textField.text isEqualToString:@""])
{
//perform anything here
}
}
}`
这时当你点击其他文本字段时,它会检查你当前的文本字段是否为空?
答案 5 :(得分:0)
使用shouldChangeCharactersInRange,因为每次点击文本字段时此方法都会调用: -
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(@"%@",textField.text);
//Your Code here
return YES;
}
答案 6 :(得分:0)
是的,您可以通过委托方法执行此操作
-(void)textFieldDidEndEditing:(UITextField *)textField
{
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"%@",textField.text);
return YES;
}