我有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類。
2視圖控制器如何相關?一個人進入另一個?是否有向用戶呈現它們的順序? – Brian
不是100%確定爲什麼這兩個viewControllers不應該相互連接,但你總是可以使用'CoreData'來存儲和檢索必要的數據。 – sCha
這些值將由用戶給出。我正在開發一個論壇,以便輸入的數據必須發送到服務器。 –