从一个viewcontroller到另一个viewController选择的多个数组元素

时间:2017-03-27 11:47:50

标签: ios iphone swift tableview segue

我从第一个vc到第二个和第二个segue从第二个到第一个从表格输入。

需要逐个选择tableview的值并将其从segue destination复制到第一个viewcontroller的文本框中,它总是在从vc2到vc2时加载viewdidload,I需要使用viewdidappear

class membersViewController: UIViewController,UITableViewDataSource,UITableViewDelegate
 {

var array = ["Gurpreet","Pulkit","Raman","Rajan","Jonish"]
var searchActive : Bool = false
 var filtered:[String] = []
 var selectedMem = ""
 override func viewDidLoad() {
 super.viewDidLoad()
       }
 @IBOutlet var tableView: UITableView!
 func numberOfSections(in tableView: UITableView) -> Int {
    return 1
      }
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return array.count
}
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCell(withIdentifier: "cell")! as UITableViewCell
    cell.textLabel?.text = array[indexPath.row]
    return cell 
     }
 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueMember"
    {
        let vc = segue.destination as! testViewController
        vc.selectedMem = selectedMem
    }
    }

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)     {
         selectedMem = array[indexPath.row]
         performSegue(withIdentifier: "segueMember", sender: nil)
      // dismiss(animated: true, completion: nil)
           }
  class testViewController: UIViewController {
         var selectedMem = ""
         override func viewDidLoad() {
         super.viewDidLoad()
         textMem?.text = selectedMem
       }
 override func viewDidAppear(_ animated: Bool) {
    textMem?.text = (textMem.text)! + selectedMem
}
    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
     }
  @IBOutlet var textMem: UITextField!

1 个答案:

答案 0 :(得分:1)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: Ind. exPath)
 {
   selectedMem = array[indexPath.row]
  performSegue(withIdentifier: "segueMember", sender: self)
  // dismiss(animated: true, completion: nil)
}

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){{

  if (segue.identifier == "segueMember") 
  {

    let viewController = segue.destinationViewController as YourViewController
   // make property in YourViewController class
    viewController.yourValue = selectedMem 
  }
}