这是我的代码:

@IBAction func moreClicked(_ sender: UIButton) {
    if sender.currentImage == #imageLiteral(resourceName: "more_off"){
        sender.setImage(#imageLiteral(resourceName: "more_on"), for: .normal)
    }
    else
    {
        sender.setImage(#imageLiteral(resourceName: "more_off"), for: .normal)
    }

}


我收到此错误:


  2018-09-05 11:03:28.708661 + 0530 musicPlayer [25450:590989]
  -[musicPlayer.SecondViewController更多:]:无法识别的选择器已发送到实例0x7fc1d4606d00
  
  2018-09-05 11:03:28.715659 + 0530 musicPlayer [25450:590989] *
  由于未捕获的异常而终止应用程序
  “ NSInvalidArgumentException”,原因:
  '-[musicPlayer.SecondViewController更多:]:无法识别的选择器已发送
  到实例0x7fc1d4606d00'
  *首先抛出调用堆栈:(0 CoreFoundation 0x00000001087ea1e6 exceptionPreprocess + 294 1 libobjc.A.dylib
  0x0000000107a36031 objc_exception_throw + 48 2 CoreFoundation
  0x000000010886b784-[NSObject(NSObject)didNotRecognizeSelector:] +
  132 3 UIKit 0x0000000108e956db
  -[UIResponder didNotRecognizeSelector:] + 295 4 CoreFoundation 0x000000010876c898 ___forwarding_ + 1432 5 CoreFoundation
  0x000000010876c278 _CF_forwarding_prep_0 + 120 6 UIKit
  0x0000000108c683e8-[UIApplication sendAction:to:from:forEvent:] + 83
    7个UIKit 0x0000000108de37a4
  -[UIControl sendAction:to:forEvent:] + 67 8 UIKit 0x0000000108de3ac1-[UIControl _sendActionsForEvents:withEvent:] + 450
    9 UIKit 0x0000000108de2a09
  -[UIControl touchesEnded:withEvent:] + 580 10 UIKit 0x0000000108cdd0bf-[UIWindow _sendTouchesForEvent:] + 2729 11 UIKit
  0x0000000108cde7c1-[UIWindow sendEvent:] + 4086 12 UIKit
  0x0000000108c82310-[UIApplication sendEvent:] + 352 13 UIKit
  0x00000001095c36af dispatchPreprocessedEventFromEventQueue + 2796
    14个UIKit 0x00000001095c62c4
  __handleEventQueueInternal + 5949 15 CoreFoundation 0x000000010878cbb1
  __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 16 CoreFoundation 0x00000001087714af
  __CFRunLoopDoSources0 + 271 17 CoreFoundation 0x0000000108770a6f __CFRunLoopRun + 1263 18 CoreFoundation
  0x000000010877030b CFRunLoopRunSpecific + 635 19图形服务
  0x000000010ff76a73 GSEventRunModal + 62 20 UIKit
  0x0000000108c67057 UIApplicationMain + 159 21音乐播放器
  0x000000010711cc27 main + 55 22 libdyld.dylib
  0x000000010cd4f955开始+ 1)libc ++ abi.dylib:终止于
  类型为NSException的未捕获异常

最佳答案

2018-09-05 11:03:28.708661 + 0530 musicPlayer [25450:590989]
  -[musicPlayer.SecondViewController更多:]:无法识别的选择器已发送到实例0x7fc1d4606d00


上面的消息表示SecondViewController希望具有称为more:的方法,但是当触发该操作并且此类中的实例尝试查找more:方法时,它无法识别该方法,并且应用程序崩溃。

我认为这里发生的事情是:您已将动作从Xib / Storyboard拖动到VC类,并将其命名为more:。之后,您已将其手动重命名(不是Refactor> Rename)为moreClicked:。这样,SecondViewController的IBAction仍指向称为more:的方法,但是您的类不再具有这种方法。

因此,基本上,您需要从xib / storyboard中删除IBAction的现有引用,并使用新方法moreClicked:重新连接它,并且所有这些都应正常工作。

关于ios - 单击按钮时将按钮的图像从图像更改为另一图像时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52177925/

10-15 14:10