草庐IT

ios - UITapGestureRecognizer 在多次点击时崩溃

coder 2023-09-04 原文

我正在以编程方式将 UIImageView 添加到 UIScrollView 中,并在 imageView 上添加点击手势。它准确地识别了 4 次点击事件,第四次它崩溃了,没有任何明确的错误消息。这是在名为 FeaturedListingDetailVC

的 Controller 中

代码:

let frame = CGRect(x: 0, y: 0, width: 128, height: 128)
_iv = UIImageView(frame: frame)
_iv.image = UIImage(named: "no_media")            
_iv.userInteractionEnabled = true

tapGesture = UITapGestureRecognizer(target: self, action: #selector(initImagePopup(_:)))
_iv.addGestureRecognizer(tapGesture)

_iv.widthAnchor.constraintEqualToConstant(CGFloat.init(128)).active = true
_iv.heightAnchor.constraintEqualToConstant(CGFloat.init(128)).active = true
hrScroll.addSubview(_iv)

点击时调用的函数:

@objc func initImagePopup(gesture: UITapGestureRecognizer) {
    print("I am tapped!!!")
}

此外,我加载目标 viewController 的方式 (即 FeaturedListingDetailVC) 确实很重要,但我不知道为什么以及如何。因为当我将我的目标 viewController 插入 UINavigationViewController 时,它会在第一次点击时崩溃,否则当我通过 presentation viewController 初始化目标 VC 时,它会在第 4 次点击时崩溃。

使用以下代码初始化时首次点击时崩溃:

let storyboard = UIStoryboard(name: "Post", bundle: nil)
let featuredVC = storyboard.instantiateViewControllerWithIdentifier("FeaturedListingDetailVC") as! FeaturedListingDetailVC
self.vc?.navigationController!.pushViewController(featuredVC, animated: true)

使用以下代码初始化时在第 4 次点击时崩溃:

let storyboard = UIStoryboard(name: "Post", bundle: nil)
let featuredVC = storyboard.instantiateViewControllerWithIdentifier("FeaturedListingDetailVC") as! FeaturedListingDetailVC
self.vc?.navigationController!.presentViewController(featuredVC, animated: true, completion: nil)

回溯:

* thread #1: tid = 0x78c3e, 0x0000000106365553 UIKit\`-[UIViewController(UIKitManual) release] + 122, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    frame #0: 0x0000000106365553 UIKit\`-[UIViewController(UIKitManual) release] + 122
    frame #1: 0xfffffffee7368160
    frame #2: 0x0000000105bf72f3 UIKit\`-[UIViewController setChildModalViewController:] + 248
    frame #3: 0x0000000105be853e UIKit\`-[UIViewController dealloc] + 1329
    frame #4: 0x0000000105f48b31 UIKit\`_UIGestureRecognizerSendTargetActions + 162
    frame #5: 0x0000000105f4519a UIKit\`_UIGestureRecognizerSendActions + 162
    frame #6: 0x0000000105f43197 UIKit\`-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 843
    frame #7: 0x0000000105f4b655 UIKit\`___UIGestureRecognizerUpdate_block_invoke898 + 79
    frame #8: 0x0000000105f4b4f3 UIKit\`_UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 342
    frame #9: 0x0000000105f38e75 UIKit\`_UIGestureRecognizerUpdate + 2634
    frame #10: 0x0000000105ac548e UIKit\`-[UIWindow _sendGesturesForEvent:] + 1137
    frame #11: 0x0000000105ac66c4 UIKit\`-[UIWindow sendEvent:] + 849
    frame #12: 0x0000000105a71dc6 UIKit\`-[UIApplication sendEvent:] + 263
    frame #13: 0x0000000105a4b553 UIKit\`_UIApplicationHandleEventQueue + 6660
    frame #14: 0x00000001046f2301 CoreFoundation\`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #15: 0x00000001046e822c CoreFoundation\`__CFRunLoopDoSources0 + 556
    frame #16: 0x00000001046e76e3 CoreFoundation\`__CFRunLoopRun + 867
    frame #17: 0x00000001046e70f8 CoreFoundation\`CFRunLoopRunSpecific + 488
    frame #18: 0x000000010bf6ead2 GraphicsServices\`GSEventRunModal + 161
    frame #19: 0x0000000105a50f09 UIKit\`UIApplicationMain + 171
  * frame #20: 0x0000000103e34b22 JaClassified\`main + 114 at AppDelegate.swift:5
    frame #21: 0x000000010886b92d libdyld.dylib\`start + 1
    frame #22: 0x000000010886b92d libdyld.dylib\`start + 1

最佳答案

听起来很奇怪,我测试了很多你的代码,没有办法,总是崩溃。

所以我只是尝试更改回调方法的名称:

 func didTapOnImagePopup(recognizer: UITapGestureRecognizer) {}

而且有效。

我真的不知道为什么,我能找到任何合理的解释。

我唯一能想到的是应该存在一个名为“initImagePopup”的内部函数......但这很奇怪。

如果有人知道的话,我真的很好奇真正的解释。

关于ios - UITapGestureRecognizer 在多次点击时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36982581/

有关ios - UITapGestureRecognizer 在多次点击时崩溃的更多相关文章

  1. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  2. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  3. Ruby Readline 在向上箭头上使控制台崩溃 - 2

    当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby​​安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少

  4. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  5. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  6. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  7. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  8. ruby-on-rails - 多次选择一个随机数,但绝不会两次选择相同的随机数 - 2

    这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:HowdoIgeneratealistofnuniquerandomnumbersinRuby?我想做的事:Random.rand(0..10).timesdoputsRandom.rand(0..10)end但如果随机数已经显示过,则无法再次显示。如何最轻松地做到这一点?

  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 - 难倒点击与 nokogiri 和 Mechanize 的链接 - 2

    也许我做错了,或者还有另一种更有效的方法。这是我的问题:我首先使用nokogiri打开一个html文档并使用其css遍历该文档,直到找到我需要单击的链接。现在我有了链接后,如何使用Mechanize来点击它?根据文档,Mechanize.new返回的对象是字符串或Mechanize::Page::Link对象。我不能使用字符串-因为可能有100个相同的链接-我只想Mechanize点击nokogiri遍历的链接。有什么想法吗? 最佳答案 找到所需的链接节点后,您可以手动创建Mechanize::Page::Link对象,然后单击它:

随机推荐