简体   繁体   English

为什么这会导致无法识别的选择器错误?

[英]Why is this causing an unrecognized selector error?

    import UIKit
import Toolbar

class ViewController: UIViewController {
  
  lazy var toolbar: Toolbar = {
    let toolbar: Toolbar = Toolbar([
      Toolbar.Item(title: "BUTTON", target: nil, action: #selector(test(_:))),
      Toolbar.Item(title: "BUTTON", target: nil, action: nil),
      Toolbar.Item(title: "BUTTON", target: nil, action: nil)
    ])
    return toolbar
  }()
  
  
  override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = .systemBackground
    self.view.addSubview(self.toolbar)
  }
  
  @objc func test(_ sender: Any) {
    print("hello")
  }
}

After I press the first toolbar button the app crashes with:在我按下第一个工具栏按钮后,应用程序崩溃了:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull test:]: unrecognized selector sent to instance 0x1de715eb0'
terminating with uncaught exception of type NSException
CoreSimulator 802.6 - Device: iPhone 13 mini (41207433-2958-4380-93E0-D0448B566794) - Runtime: iOS 15.4 (19E240) - DeviceType: iPhone 13 mini
(lldb) 

I can't figure out how I am calling the test function incorrectly?我不知道我是如何错误地调用测试 function 的?

Instead of:代替:

Toolbar.Item(title: "BUTTON", target: nil, action: #selector(test(_:))),

Try:尝试:

Toolbar.Item(title: "BUTTON", target: self, action: #selector(test(_:))),

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

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