我在主视图控制器中有一个名为ViewController.swift的按钮,需要将按钮的值带到ToDoListViewController。但它显示一个错误:
[ToDoCalender.ViewController.DatebtnTapped:]: unrecognized selector sent to instance 0x7fdb6b808270.
我尝试移除按钮的所有插座,然后重新设置连接。但它仍然无法正常工作。之后,我尝试在DateCollectionViewCell中使用委托并在DateCollectionViewCell中调用DatebtnTapped。在DatebtnTapped中,通过协议委托调用了在ViewController中定义的另一个函数dtbtntapped。但这并不能解决问题。
我正在为我大学课程下的一个项目做这件事。我是初学者。
我的原始代码:
ViewController.swift:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
...
@IBAction func DatebtnTapped(cell: DateCollectionViewCell) {
let daydata = cell.Datebtn.currentTitle!
let mdata = currentMonth
let ydata = year
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ToDoListViewController") as! ToDoListViewController
vc.day = daydata
vc.month = mdata
vc.year = ydata
self.navigationController?.pushViewController(vc, animated: true)
}
}
DateCollectionViewCell.swift:
import UIKit
class DateCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var Datebtn: UIButton!
}
ToDoListViewController.swift:
import UIKit
class ToDoListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var ListTableView: UITableView!
@IBOutlet weak var date: UILabel!
var day = ""
var month = ""
var year:Int = 0
...
override func viewDidLoad() {
super.viewDidLoad()
date.text = " \(day) - \(month) - \(year)"
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这里,Datebtn和Datebtntapped表示相同的按钮。我正在使用xcode10和macOS Catalina。如何解决此问题?
替换
@IBAction func DatebtnTapped(cell: DateCollectionViewCell) {
with
@IBAction func DatebtnTapped(_ btn : UIButton) {