0
votes

there is a Table View to show phone contact . i want to send phone number and email to another View Controller when Long Pressed the Cell . Long Press Work Correctly But I cant Pass Data to another View Controller .

enter image description here

VC 1 :

  override func viewDidLoad() {
        super.viewDidLoad()

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

Long Press Method for table view cell :

  @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.
    }
2

2 Answers

0
votes

Get phone number and email from cell object

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

code would be like

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


        }
    }
}
0
votes

I don't see when your trying to pass the data.. you have quite a few way to perform that action first you can use delegation to achieve passing the data

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...
}
}

its not clear to me if the actionVC is the one you want to pass the data to but if so you have an instance.. just set the properties but ill still recommend sticking with the delegation pattern

actionVC.strPhoneNumber = cell.strPhoneNumber

or use a segue

    self.performSegue(withIdentifier: "yourIdentifier", sender: arr[indexPath.row])

use prepare for segue to create an instance to set his properties according to the sender..

 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

    }