Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法识别的选择器作为按钮发送到集合视图单元格的实例,该按钮将数据从该按钮传送到另一个视图控制器_Ios_Swift_Xcode10_Macos Catalina - Fatal编程技术网

Ios 无法识别的选择器作为按钮发送到集合视图单元格的实例,该按钮将数据从该按钮传送到另一个视图控制器

Ios 无法识别的选择器作为按钮发送到集合视图单元格的实例,该按钮将数据从该按钮传送到另一个视图控制器,ios,swift,xcode10,macos-catalina,Ios,Swift,Xcode10,Macos Catalina,我在主视图控制器中有一个名为ViewController.swift的按钮,需要将该按钮的值传递给olistviewcontroller。 但它显示了一个错误: [ToDoCalender.ViewController.DatebtnTapped:]: unrecognized selector sent to instance 0x7fdb6b808270. 我已尝试移除按钮的所有插座,并再次设置连接。但它仍然不起作用。之后,我尝试在DateCollectionViewCell中使用委托,并

我在主视图控制器中有一个名为ViewController.swift的按钮,需要将该按钮的值传递给olistviewcontroller。 但它显示了一个错误:

[ToDoCalender.ViewController.DatebtnTapped:]: unrecognized selector sent to instance 0x7fdb6b808270.
我已尝试移除按钮的所有插座,并再次设置连接。但它仍然不起作用。之后,我尝试在DateCollectionViewCell中使用委托,并调用DateCollectionViewCell中的DateBTNTAP。在DatebtnTapped中,通过协议委托调用在我的ViewController中定义的另一个函数dtbtntapped。但这并没有解决问题

我做这个是为了我大学的一门课程下的一个项目。我是初学者

我的原始代码:

ViewController.swift:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

   ...
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Calendar", for: indexPath) as! DateCollectionViewCell
        ...
}
   ...
    @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)
    }
}
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()
    }

}
DateCollectionViewCell.swift:

import UIKit

class DateCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var Datebtn: UIButton!
}
ToDoListViewController.swift:

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

   ...
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Calendar", for: indexPath) as! DateCollectionViewCell
        ...
}
   ...
    @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)
    }
}
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) {

参数的类型应为
UIButton
使用其标记访问表的数据源


“使用它的标签访问数据”是什么意思。我是初学者。所以,很多事情我都不清楚。我已经试过你给出的解决方案了。但它无法访问DateBTntaped函数中DateCollectionViewCell的Datebtn。我已根据您的建议更改了collectionview函数中的单元格代码和ViewController.swift中的DateBTntaped代码。但它仍然显示“使用未解决的标识符‘cell’”@Sh_Khani已经尝试并解决了一些问题。但它仍然显示相同的错误@objc func DatebtnTapped(sender:UIButton){let daydata=sender.currentTitle!``和:``let cell=collectionView.dequeueReusableCell(withReuseIdentifier:“Calendar”,for:indexath)为!DateCollectionViewCell.Datebtn.tag=indexath.row cell.Datebtn.addTarget(self,#选择器(DatebtnTapped(sender:)),for:.touchind)“``没有。我没有。但是尝试使用deligate协议从DateCollectionViewCell调用该操作。但是它也显示了相同的错误。@KishanSuthary您在tableview单元格中有use按钮??该按钮在collectionview单元格@KishanSuthar中使用