2017-09-02 91 views
0

我有2個視圖控制器和一個快速文件。我希望將數據從一個視圖控制器傳遞給另一個,而不使用segue或呈現其他視圖控制器。從視圖控制器的文本字段獲取值

視圖控制器:

import UIKit 
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

var questions:[Question] = [] 

var sub = "" 
var send = "" 
var det = "" 

@IBOutlet weak var questionsender: UISegmentedControl! 
@IBOutlet weak var tableView: UITableView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
} 
override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    self.tableView.reloadData() 
} 

func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return questions.count 
} 
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 50 
} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "forumcell", for: indexPath) as! ForumTableViewCell 
    cell.questionsubject.text = questions[indexPath.row].subject 
    cell.sender.text = questions[indexPath.row].sender 
    return cell 
} 


} 

夫特文件:

import Foundation 
class Question 
{ 
var subject = "" 
var descript = "" 
var sender = "" 
init(subject : String, descrip: String,sender : String) 
{ 
    self.sender=sender 
    self.descript=descrip 
    self.subject=subject 
} 
} 

第二視圖控制器:

import UIKit 

class AddVC: UIViewController,UITextFieldDelegate { 
var qsender = "" 
var subject = "" 
var details = "" 

@IBAction func postBttn(_ sender: Any) { 
    if questiondetails.text == "" || question_sender.text == "" || question_subject.text == "" 
    { 
     let alert = UIAlertController(title: "Invalid!", message: "One of the fields has not been entered", preferredStyle: .alert) 

     let bttn = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
     alert.addAction(bttn) 

    } 
    else 
    { 
     qsender = question_sender.text 
     subject = question_subject.text 
     details = questiondetails.text 
     let q=Question(subject: subject, descrip: details, sender: qsender) 
     let v = ViewController() 
     v.send = qsender 
     v.sub = subject 
     v.det = details 
     dismiss(animated: true, completion: nil) 
    } 
} 
@IBAction func closeBttn(_ sender: Any) { 
    dismiss(animated: true, completion: nil) 
} 

@IBOutlet weak var question_subject: UITextView! 
@IBOutlet weak var question_sender: UITextView! 
@IBOutlet weak var closeBttn: UIButton! 

@IBOutlet weak var questiondetails: UITextView! 
override func viewDidLoad() { 
    super.viewDidLoad() 
    question_subject.placeholderText="Sender" 
    question_subject.placeholderText="Subject" 
    questiondetails.placeholderText="Description" 
    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(swiped(_ :))) 
    swipeDown.direction = .down 
    self.view.addGestureRecognizer(swipeDown) 

} 
func swiped(_ gesture:UISwipeGestureRecognizer) 
{ 
    dismiss(animated: true, completion: nil) 
} 
} 

我爲視圖控制器創建的物體V不能更新通過所述數據成員AddVC類。

+0

2視圖控制器如何相關?一個人進入另一個?是否有向用戶呈現它們的順序? – Brian

+0

不是100%確定爲什麼這兩個viewControllers不應該相互連接,但你總是可以使用'CoreData'來存儲和檢索必要的數據。 – sCha

+0

這些值將由用戶給出。我正在開發一個論壇,以便輸入的數據必須發送到服務器。 –

回答

0

此行添加到ViewController中:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 
    static let viewControllerSegueIdentifier: String = "ViewController" 

確保viewControllerSegueIdentifier串您SEGUE標識符匹配。

然後讓這些編輯AddVC:

@IBAction func postBttn(_ sender: Any) { 
     if questiondetails.text == "" || question_sender.text == "" || question_subject.text == "" 
     { 
      let alert = UIAlertController(title: "Invalid!", message: "One of the fields has not been entered", preferredStyle: .alert) 

      let bttn = UIAlertAction(title: "Ok", style: .cancel, handler: nil) 
      alert.addAction(bttn) 

     } 
     else 
     { 
      qsender = question_sender.text 
      subject = question_subject.text 
      details = questiondetails.text 
      dismiss(animated: true, completion: { 
       goToAddScreen(segueIdentifier: ViewController.viewControllerSegueIdentifier) 
      }) 
     } 
} 

然後加入這2種方法:

func goToAddScreen(segueIdentifier: String) { 
     performSegue(withIdentifier: segueIdentifier, sender: self) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == ViewController.viewControllerSegueIdentifier { 
      guard let controller = segue.destination as? ViewController else { 
       return 
      } 
      let q=Question(subject: subject, descrip: details, sender: qsender) 
      controller.questions.append(q) 
     } 
} 
+0

仍然沒有工作...值未在視圖控制器中更新 –

+0

做了一個編輯,我沒有意識到你已經不在了使用這些變量「var sub =」「 \t var send =」「 \t var det =」「」。現在試試 – Brian

0

在視圖控制器:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    let destination = segue.destination as! AddVC 
    destination.callbackFromAddVC = { stringData in 
     // use stringData 
    } 
} 
在AddVC

var callbackFromAddVC: ((String) -> Void)? = nil 

func swiped(_ gesture:UISwipeGestureRecognizer) 
{ 
    self.callbackFromAddVC?("myStringData that needs to be transferred") 
    dismiss(animated: true, completion: nil) 

    self.callbackFromAddVC = nil 
} 
相關問題