简体   繁体   English

iOS UITextField值,无需点击“返回”按钮

[英]iOS UITextField value without tapping on RETURN button

Is it possible to take UITextField value without tapping RETURN button?. 是否可以在不点击RETURN按钮的情况下获取UITextField值? If I type something in LOGIN UITextField and then tap on PASSWORD UITextField , it looks like LOGIN value is empty, however if I type something in LOGIN, and then tap RETURN everything's fine. 如果我在LOGIN UITextField中键入一些内容,然后点击PASSWORD UITextField ,则看起来LOGIN值是空的,但是,如果我在LOGIN中键入某些内容,然后点击RETURN,一切都很好。

Without tapping on RETURN http://gyazo.com/2ca0f263275fd65ae674233f34d90280 不点击返回http://gyazo.com/2ca0f263275fd65ae674233f34d90280

With tapping on RETURN http://gyazo.com/9ccc39ba7080b6b6344454ec757d3c0f 点击返回http://gyazo.com/9ccc39ba7080b6b6344454ec757d3c0f

Here's my code: 这是我的代码:

TextInputTableViewCell.m 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 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);

...

You can simply use this... 您可以简单地使用此...

 -(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
         }
    }
    }

You can use : 您可以使用 :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

} }

These are UITextFieldDelegate methods. 这些是UITextFieldDelegate方法。 But there should be a better way than changing the dictionary key each time user presses a key. 但是应该有一个比每次用户按一个键都更改字典键更好的方法。 Set textField object as a property of TextInputTableViewCell and reach that textField object in didSelectRowAtIndexPath . textField对象设置为TextInputTableViewCellproperty ,并在didSelectRowAtIndexPath到达该textField对象。

Note : shouldChangeCharactersInRange will have the text before adding last action. 注意: shouldChangeCharactersInRange将具有添加最后一个动作之前的文本。 If you pressed "a" for example, when flow enters this function it textField.text won't have it. 例如,如果按下“ a”,则在流程进入此功能时, textField.text将不具有此功能。 Please see the documentation. 请参阅文档。

If you tap the textfield from login to password, in the textFieldDidBeginEditing method, save the text entered in login text field to an NSString or NSUserDefaults as per your wish. 如果从登录名到密码点击文本字段,则在textFieldDidBeginEditing方法中,根据需要将在登录文本字段中输入的文本保存到NSString或NSUserDefaults中。

-(void)textFieldDidBeginEditing:(UITextField *)textField
{

 if(textfield == passwordTextField )
{
[[NSUserDefaults standardUserDefaults]setValue: [NSString stringWithFormat:@"%@",loginTextField.text]forKey:@"LOGINCREDENTIALS"];
}
}

This will automatically save your userName string into userDefaults as soon as to tap the Password textfield. 点击“密码”文本字段后,这将自动将您的userName字符串保存到userDefaults中。

you can access the stored string from the userDefaults as: 您可以通过以下方式从userDefaults访问存储的字符串:

[[NSUserDefaults standardUserDefaults]valueForKey:@"LOGINCREDENTIALS"];

Hope this is useful to you. 希望这对您有用。

If you change: 如果您更改:

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

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

    return NO;
}

to: 至:

-(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;
}

everything should work as expected 一切都应该按预期进行

yes it is possible,you can perform 是的,有可能,你可以执行

- (void)textFieldDidEndEditing:(UITextField *)textField
{

`if (textField == your textfield here)
{

if ([textField.text isEqualToString:@""])
{

//perform anything here

}
}
}`

here when you'll click on other textfield, it will check your present textfield whether it is empty? 在这里,当您单击其他文本字段时,它将检查您当前的文本字段是否为空?

Use shouldChangeCharactersInRange as this method calls every time you tapped on text field:- 每次在文本字段上轻按时,均应使用shouldChangeCharactersInRange,因为此方法会调用:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range   replacementString:(NSString *)string {
 NSLog(@"%@",textField.text);
 //Your Code here
     return YES;
     }

Yes there are delegate methods by which you can do this 是的,您可以通过委托方法来执行此操作

-(void)textFieldDidEndEditing:(UITextField *)textField
{
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range   replacementString:(NSString *)string 
{

 NSLog(@"%@",textField.text);

 return YES;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM