Swift 在表格视图单元格中长按时传递数据

Swift 在表格视图单元格中长按时传递数据,swift,xcode,tableview,Swift,Xcode,Tableview,有一个表视图显示电话联系人。当长按手机时,我想向另一个视图控制器发送电话号码和电子邮件。长按可正常工作,但我无法将数据传递到其他视图控制器 VC 1: override func viewDidLoad() { super.viewDidLoad() let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress)) tbMain.

有一个表视图显示电话联系人。当长按手机时,我想向另一个视图控制器发送电话号码和电子邮件。长按可正常工作,但我无法将数据传递到其他视图控制器

VC 1:

  override func viewDidLoad() {
        super.viewDidLoad()

        let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
        tbMain.addGestureRecognizer(longPress)
}
表格视图单元格的长按方式:

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


        }
    }
}

我看不出你什么时候试图传递数据。。您有很多方法来执行该操作,首先您可以使用委派来实现数据的传递

protocol YourDelegate : class { 
func passData(phoneNumber: String, email: String)
}
weak var delegate: YourDelegate?


@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.delegate = self
**delegate?.passData(cell.strPhoneNumber, cell.strEmail)**

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


        }
    }
}



class ???: UIViewController, YourDelegate {
 var strPhoneNUmber : String!
 var strEmail : String!

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

func passData(phoneNumber: String, email: String) {
handle...
}
}
我不清楚actionVC是否是您要将数据传递给的对象,但如果是,您有一个实例。。只需设置属性,但我仍然建议坚持委托模式

actionVC.strPhoneNumber = cell.strPhoneNumber
或者使用segue

    self.performSegue(withIdentifier: "yourIdentifier", sender: arr[indexPath.row])
使用prepare for segue创建一个实例,以根据发件人设置其属性

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destvc = segue.destination as? YourClass
        destvc.phoneNumber = sender.phoneNumber as? String
        destvc.email = sender.email as? String

    }

cell.strPhoneNUmber和cell.strEmail不起作用,因为它们将在VC 2add tableViewcell类代码中定义,我将为您提供确切的代码。