如何使用故事板将字符串传递给另一个控制器(textViewController)Swift 3

时间:2017-03-04 18:35:49

标签: swift textview tableview

  1. 创建模型
  2. class model: NSObject {
        var title: String = ""
    }
    
    1. in method print works
    2. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell =  tableView.dequeueReusableCell(withIdentifier: "itemCell") as! itemTableViewCell
          let person = items[indexPath.row]
          cell.itemLabel.text = person.value(forKey: "name") as? String
          m.title = cell.itemLabel.text!
          print(m.title)
      
          return cell
      }
      
      1. 字符串不起作用
      2. class textViewController: UIViewController {
        
            @IBOutlet var textView: UITextView!
        
            override func viewDidLoad() {
                super.viewDidLoad()
        
               let V = ViewController()
                textView.text = V.m.title
            }
        
         }
        

        图片看这里

        render screenshot 1 render screenshot 2

1 个答案:

答案 0 :(得分:0)

您必须使用以下方法将字符串传递给新的视图控制器:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "AdYourIdentifierFromStoryboardHere"
    {
        if let destinationVC = segue.destinationViewController as? textViewController {
            destinationVC.title = title
        }
    }
}

这篇文章将帮助您http://www.codingexplorer.com/segue-swift-view-controllers/