jjzjj

objective-c - 自定义 UITableViewCell + UITextField 问题

coder 2024-01-15 原文

我遇到了一个我似乎无法弄清楚的问题。

我的设置:

  1. 其中包含多个文本字段的自定义 UITableViewCell。类似于 Apple 在为联系人中的某人添加新地址时的设置方式。
  2. 带有自定义单元格的表格显示为模态视图。此 TableView 还有其他通用单元格(即不是 1. 中所述的自定义单元格)。
  3. 呈现的模态视图在顶部有一个工具栏,其中包含一个完成按钮。按下完成后,模态视图将回调发送回其委托(delegate)并关闭。

我遇到的问题: 当用户选择一个 UITextField 并开始编辑时,会出现 UIKeyboard(显然对吗?)。如果键盘打开,并且用户在工具栏上选择“完成”(如 3. 中所述),应用程序会崩溃,因为父 View 试图在键盘打开时关闭模态视图(包含键盘的模态视图) .

高水平看起来它应该像这样工作:

用户点击工具栏中的“完成”-> 检查 UIKeyboard 是否打开 -> 如果打开,则关闭,否则关闭模态视图

但这并不容易。

我一直在努力寻找可行的解决方法,但似乎没有任何效果。

如有任何建议,我们将不胜感激。

编辑

这是崩溃日志:

2011-12-22 16:03:09.021 Renshaw[2308:5f43] bool _WebTryThreadLock(bool), 0xad7a9e0: Tried     to obtain the web lock from a thread other than the main thread or the web thread. This may be     a result of calling to UIKit from a secondary thread. Crashing now...
1   _ZL17_WebTryThreadLockb
2   WebThreadLock
3   -[UITextRangeImpl isEmpty]
4   -[UITextRange(UITextSelectionAdditions) _isCaret]
5   -[UITextSelectionView setCaretBlinks:]
6   -[UIKeyboardImpl setCaretBlinks:]
7   -[UIKeyboardImpl setDelegate:force:]
8   -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:]
9   -[UIResponder _finishResignFirstResponder]
10  -[UIResponder resignFirstResponder]
11  -[UITextField resignFirstResponder]
12  -[UIView(UITextField) endEditing:]
13  -[UIWindowController _prepareKeyboardForTransition:fromView:]
14  -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
15  -[UIViewController _dismissViewControllerWithTransition:from:completion:]
16  -[UIViewController dismissViewControllerWithTransition:completion:]
17  -[UIViewController dismissViewControllerWithTransition:completion:]
18  -[ListingsViewController postSearch:allObjects:constrainedList:]
19  -[FilterViewController searchObjects]
20  -[NSObject performSelector:withObject:]
21  -[MBProgressHUD launchExecution]
22  -[NSThread main]
23  __NSThread__main__
24  _pthread_start
25  thread_start
[Switching to process 10499 thread 0x2903]
[Switching to process 10499 thread 0x2903]

最佳答案

线索在堆栈跟踪中:

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

您正在从辅助线程调用 UIKit 方法。您必须在非主线程上执行某些操作,例如调用 resignFirstResponder。有关详细信息,请参见此处:

更新:

事实证明,解决方案是使用以下方式:

dispatch_async(dispatch_get_main_queue(), ^{ <code_here> });

包装关闭 View 的调用,以便 UIKit 方法在主线程上运行。

关于objective-c - 自定义 UITableViewCell + UITextField 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8609895/

有关objective-c - 自定义 UITableViewCell + UITextField 问题的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  3. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  4. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  5. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  6. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  7. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  8. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  9. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  10. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

随机推荐