ios - 从 View Controller 的文本字段获取值

标签 ios swift

我有 2 个 View Controller 和一个 swift 文件。我希望将数据从一个 View Controller 传递到另一个 View Controller ,而不使用 segue 或呈现另一个 View Controller 。

View Controller :

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
}


}

Swift 文件:

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

第二个 View Controller :

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

我为View Controller创建的对象v无法通过AddVC类更新数据成员。

最佳答案

在 View Controller 中:

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
}

关于ios - 从 View Controller 的文本字段获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46011548/

相关文章:

iphone - iPad 和 iPhone 的启动方向不同?

ios - NSLocalizedString 中的 "%"符号

ios - 在 Swift 中更新设备旋转时的 UICollectionView 宽度

ios - 调整 UICollectionView 中特定部分的插图

ios - 网格的简单布局数学

ios - RTCPeerConnectionFactory.peerConnectionWithConfiguration 导致 IOS 模拟器崩溃

ios - 核心图:减少条形图iOS中条形之间的距离

swift - 我不了解 Swift Unicode 标量基础知识。我缺少什么

swift - 了解 removeRange(_ :) documentation

ios - xcode 不允许我将应用程序上传到我的手机