Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

ios - 无法识别的选择器发送到按钮的实例,该选择器将数据从按钮传送到另一个视图控制器(Unrecognized selector sent to instance for a button which takes data from the button to another view controller)

I have a button in main view controller named ViewController.swift and need to take the value of the button to ToDoListViewController.

(我在名为ViewController.swift的主视图控制器中有一个按钮,需要将按钮的值用于ToDoListViewController。)

But it's showing an error :

(但是它显示了一个错误:)

[ToDoCalender.ViewController.DatebtnTapped:]: unrecognized selector sent to instance 0x7fdb6b808270.

I've tried removing all outlets of the button and set connections again.

(我尝试过删除按钮的所有插座并再次设置连接。)

But it still not working.

(但它仍然无法正常工作。)

After that i've tried to use delegate in DateCollectionViewCell and call the DatebtnTapped in the DateCollectionViewCell.

(之后,我尝试在DateCollectionViewCell中使用委托并在DateCollectionViewCell中调用DatebtnTapped。)

Within DatebtnTapped another function dtbtntapped which is defined in my ViewController is called via protocol delegate.

(在DatebtnTapped中,通过协议委托调用了在ViewController中定义的另一个函数dtbtntapped。)

But it didn't resolve the problem.

(但这并不能解决问题。)

I'm doing this for a project under a course of my university.

(我正在为我大学课程下的一个项目做这件事。)

I'm a beginner.

(我是初学者。)

My original code:

(我的原始代码:)

ViewController.swift:

(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)
    }
}

DateCollectionViewCell.swift:

(DateCollectionViewCell.swift:)

import UIKit

class DateCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var Datebtn: UIButton!
}

ToDoListViewController.swift:

(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()
    }

}

Here, Datebtn and Datebtntapped indicating the same button.

(这里,Datebtn和Datebtntapped表示相同的按钮。)

I'm using xcode10 and macOS Catalina.

(我正在使用xcode10和macOS Catalina。)

How to fix this problem?

(如何解决这个问题?)

  ask by nowshin tasnim translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Replace

(更换)

@IBAction func DatebtnTapped(cell: DateCollectionViewCell) {

with

(与)

@objc func DatebtnTapped(_ btn : UIButton) {
   let clickedItem = arr[btn.tag]

the parameter should be of type UIButton use it's tag to access the data source of the table

(该参数应该是UIButton类型,使用它的标记来访问表的数据源)

and

(和)

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Calendar", for: indexPath) as! DateCollectionViewCell
cell.Datebtn.tag = indexPath.row
cell.Datebtn.addTarget(self,#selector(DatebtnTapped),for:.touchUpInside)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...