簡體   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")
  }
}

在我按下第一個工具欄按鈕后,應用程序崩潰了:

*** 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) 

我不知道我是如何錯誤地調用測試 function 的?

代替:

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

嘗試:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM