草庐IT

iOS UITextField 值无需点击 RETURN 按钮

coder 2024-01-15 原文

是否可以在不点击 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/

有关iOS UITextField 值无需点击 RETURN 按钮的更多相关文章

  1. jquery - 我的 jquery AJAX POST 请求无需发送 Authenticity Token (Rails) - 2

    rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送

  2. ruby - Ruby 是否使用 $stdout 来写入 puts 和 return 的输出? - 2

    我想知道Ruby用来在命令行打印这些东西的输出流:irb(main):001:0>a="test"=>"test"irb(main):002:0>putsatest=>nilirb(main):003:0>a=>"test"$stdout是否用于irb(main):002:0>和irb(main):003:0>?而且,在这两次调用之间,$stdout的值是否有任何变化?另外,有人能告诉我打印/写入这些内容的Ruby源代码吗? 最佳答案 是的。而且很容易向自己测试/证明。在命令行试试这个:ruby-e'puts"foo"'>test.

  3. ruby-on-rails - Rails 单选按钮 - 模型中多列的一种选择 - 2

    我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模

  4. ruby - 为什么 return 关键字会导致我的 'if block' 出现问题? - 2

    下面的代码工作正常:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson)do|key,oldv,newv|ifkey==:aoldvelsifkey==:bnewvelsekeyendendputskerson.inspect但是如果我在“ifblock”中添加return,我会得到一个错误:person={:a=>:A,:b=>:B,:c=>:C}berson={:a=>:A1,:b=>:B1,:c=>:C1}kerson=person.merge(berson

  5. ruby-on-rails - 在 Ruby 或 Rails 中,hash.merge({ :order => 'asc' }) can return a new hash with a new key. 什么可以返回带有已删除键的新散列? - 2

    在Ruby(或Rails)中,我们可以做到new_params=params.merge({:order=>'asc'})现在new_params是一个带有添加键:order的散列。但是是否有一行可以返回带有已删除key的散列?线路new_params=params.delete(:order)不会工作,因为delete方法返回值,仅此而已。我们必须分3步完成吗?tmp_params=paramstmp_params.delete(:order)returntmp_params有没有更好的方法?因为我想做一个new_params=(params[:order].blank?||para

  6. ruby-on-rails - 如何从按钮或链接单击的 View 调用 Rails 方法 - 2

    基本上,我试图在用户单击链接(或按钮或某种类型的交互元素)时执行Rails方法。我试着把它放在View中:但这似乎没有用。它最终只是在用户甚至没有点击“添加”链接的情况下调用该函数。我也用link_to试过了,但也没用。我开始认为没有一种干净的方法可以做到这一点。无论如何,感谢您的帮助。附言。我在ApplicationController中定义了该方法,它是一个辅助方法。 最佳答案 View和Controller是相互独立的。为了使链接在Controller内执行函数调用,您需要对应用程序中的端点执行ajax调用。该路由应调用rub

  7. ruby-on-rails - 如何在 Rails 中添加禁用的提交按钮 - 2

    我在ruby​​表单中有一个提交按钮f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id"我想在不使用任何javascript的情况下通过ruby​​禁用此按钮 最佳答案 添加disabled:true选项。f.submitbtn_text,class:"btnbtn-onemgt12mgb12",id:"btn_id",disabled:true 关于ruby-on-rails-如何在Rails中添加禁用的提交按钮,我们在St

  8. ruby-on-rails - 从 ActiveAdmin has_many 表单助手中删除 "Add new"按钮 - 2

    我在事件管理员编辑页面中有嵌套资源,但我只想允许管理员编辑现有资源的内容,而不是添加新的嵌套资源。我的代码看起来像这样:formdo|f|f.inputsdof.input:authorf.input:contentf.has_many:commentsdo|comment_form|comment_form.input:contentcomment_form.input:_destroy,as::boolean,required:false,label:'Remove'endendf.actionsend但它在输入下添加了“添加新评论”按钮。我怎样才能禁用它,并只为主窗体保留f.ac

  9. ruby - Sinatra:点击 URL 时运行 ruby​​ 代码 - 2

    我想在每次访问url/code时运行一个脚本(code.rb)。如何运行脚本?require'sinatra'get'/'do#runthescriptend 最佳答案 要么fork另一个进程:system('rubycode.rb')...或者简单地将脚本加载到当前上下文中:load'code.rb'#*not*require 关于ruby-Sinatra:点击URL时运行ruby​​代码,我们在StackOverflow上找到一个类似的问题: https:

  10. ruby - 读取 zip 存档中的文件,无需解压缩存档 - 2

    我有一个包含100多个zip文件的目录,我需要读取zip文件中的文件以进行一些数据处理,而无需解压缩存档。是否有一个Ruby库可以在不解压缩文件的情况下读取zip存档中的文件内容?使用rubyzip报错:require'zip'Zip::File.open('my_zip.zip')do|zip_file|#Handleentriesonebyonezip_file.eachdo|entry|#Extracttofile/directory/symlinkputs"Extracting#{entry.name}"entry.extract('here')#Readintomemoryc

随机推荐