swift - 在表格 View 单元格中长按时传递数据

标签 swift xcode tableview

有一个表格 View 来显示电话联系人。我想在长按单元格时将电话号码和电子邮件发送到另一个 View Controller 。长按工作正常,但我无法将数据传递到另一个 View Controller 。

enter image description here

VC 1:

  override func viewDidLoad() {
        super.viewDidLoad()

        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
        tbMain.addGestureRecognizer(longPress)
}

表格 View 单元格的长按方法:

  @objc func longpress(sender: UILongPressGestureRecognizer) {

        if sender.state == UIGestureRecognizer.State.began {
            let touchPoint = sender.location(in: tbMain)
            if tbMain.indexPathForRow(at: touchPoint) != nil {

                let cell = tbMain.dequeueReusableCell(withIdentifier: "testCell") as! NewContactCell

                print("Long press Pressed:)")

                self.actionVC = self.storyboard!.instantiateViewController(withIdentifier: "ActionsViewController") as? ActionsViewController

                UIView.transition(with: self.view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
                    self.view.addSubview( self.actionVC.view)
                }, completion: nil)


            }
        }
    }

VC 2:

 internal var strPhoneNUmber : String!
 internal var strEmail : String!

    override func viewDidLoad() {
        super.viewDidLoad()
        print("Phone: \(strPhoneNUmber!)")
        // Do any additional setup after loading the view.
    }

最佳答案

从单元对象获取电话号码和电子邮件

self.actionVC.strPhoneNUmber = cell.strPhoneNUmber // get phone number from cell object 
self.actionVC. strEmail = cell.strEmail // get email from cell object 

代码就像

@objc func longpress(sender: UILongPressGestureRecognizer) {
    if sender.state == UIGestureRecognizer.State.began {
        let touchPoint = sender.location(in: tbMain)
        if tbMain.indexPathForRow(at: touchPoint) != nil {

            let cell = tbMain.dequeueReusableCell(withIdentifier: "testCell") as! NewContactCell

            print("Long press Pressed:)")

            self.actionVC = self.storyboard!.instantiateViewController(withIdentifier: "ActionsViewController") as? ActionsViewController
            **self.actionVC.strPhoneNUmber = cell.strPhoneNUmber // get phone number from cell object 
            self.actionVC. strEmail = cell.strEmail // get email from cell object**
            UIView.transition(with: self.view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
                self.view.addSubview( self.actionVC.view)
            }, completion: nil)


        }
    }
}

关于swift - 在表格 View 单元格中长按时传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58678026/

相关文章:

swift - 尝试以编程方式使用 Assets 中图像的未解析标识符

php - Swift 代码不更新 MySQL 数据库

ios - UITableViewCells 初始加载 View /显示问题

swift - 如何使用 Swift 打开本地保存的 PDF 文件

swift - 如何使用可变数据源在 TableView 中按字母顺序制作节标题

ios - Mapbox 图像标记

ios - 快速更改 UImenu 的位置

ios - 不区分大小写的SearchBar Xcode

ios - UITextView 无法在以编程方式创建的 UITableView 中关闭键盘

ios - 如何根据运行时系统iOS版本只覆盖一个方法?