首页 > 解决方案 > 如何使用 Swift 4 将数据从弹出子视图传递到视图控制器

问题描述

我正在尝试从我UITextField的子视图中的 (taskNameField) 子视图传递文本数据taskCreator。不知道如何在子视图之外获取这些数据,以便我可以将其添加到我的任务数组中。任何帮助将不胜感激。

import UIKit

var userTasks: [Task] = []

class PlannerViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UITextFieldDelegate {

static let viewHeight = UIScreen.main.bounds.height
static let viewWidth = UIScreen.main.bounds.width

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 110, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: view.frame.width - 30, height: 60)

    let myCollectionView:UICollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    myCollectionView.dataSource = self
    myCollectionView.delegate = self
    myCollectionView.register(TaskCell.self, forCellWithReuseIdentifier: "cellID")
    myCollectionView.backgroundColor = UIColor.darkGray
    self.view.addSubview(myCollectionView)

    let addButton = UIButton(frame: CGRect(x: view.frame.width - 70, y: view.frame.height - 120, width: 50, height: 50))
    addButton.backgroundColor = UIColor.red
    addButton.setBackgroundImage(#imageLiteral(resourceName: "PlusIcon"), for: UIControlState.normal)
    addButton.layer.cornerRadius = 25
    addButton.addTarget(self, action: #selector(addTask), for: .touchUpInside)
    self.view.addSubview(addButton)
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellID", for: indexPath) as! TaskCell
    cell.layer.masksToBounds = true
    cell.layer.cornerRadius = 10
    cell.backgroundColor = UIColor.black
    return cell
}

@IBAction func addTask(sender: UIButton) {
    view.addSubview(taskCreator)
}

@IBAction func saveTask(sender: UIButton) {
    taskCreator.removeFromSuperview()
}

let taskCreator: UIView = {
    let object = UIView()
    object.frame = CGRect(x: 10, y: 50, width: viewWidth - 20, height: viewHeight - 350)
    object.layer.cornerRadius = 10
    object.backgroundColor = UIColor.lightGray

    let taskNameField: UITextField = {
        let field = UITextField()
        field.frame = CGRect(x: 20, y: 20, width: object.frame.width - 40, height: 30)
        field.layer.cornerRadius = 5
        field.placeholder = "New Task"
        field.textAlignment = NSTextAlignment(rawValue: 3)!
        field.becomeFirstResponder()
        return field
    }()

    let doneButton: UIButton = {
        let done = UIButton()
        done.frame = CGRect(x: viewWidth / 2 - 55, y: 200, width: 90, height: 30)
        done.layer.cornerRadius = 5
        done.backgroundColor = UIColor.blue
        done.addTarget(self, action: #selector(saveTask), for: .touchUpInside)
        return done
    }()

    object.addSubview(taskNameField)
    object.addSubview(doneButton)

    return object
}()

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

标签: iosswiftuitextfield

解决方案


如果你想得到 a 的UITextField就这么简单。

var textFieldValue = taskNameField.text

但是,如果您在用户在文本字段中输入内容后立即需要它,那么您需要使用您必须遵守的委托UITextFieldDelegate方法UIViewController

func textFieldDidEndEditing(textField: UITextField) {
    var textFieldValue = textField.text
    // Do something with the value like adding it to a Task object
}

如果您需要访问视图之外的文本字段,则需要在外部声明它。还要声明它们,lazy以便仅在需要时创建它们。

lazy var doneButton: UIButton = {
    let done = UIButton()
    done.frame = CGRect(x: viewWidth / 2 - 55, y: 200, width: 90, height: 30)
    done.layer.cornerRadius = 5
    done.backgroundColor = UIColor.blue
    done.addTarget(self, action: #selector(saveTask), for: .touchUpInside)
    return done
}()

lazy var taskNameField: UITextField = {
    let field = UITextField()
    field.frame = CGRect(x: 20, y: 20, width: object.frame.width - 40, height: 30)
    field.layer.cornerRadius = 5
    field.placeholder = "New Task"
    field.textAlignment = NSTextAlignment(rawValue: 3)!
    field.becomeFirstResponder()
    return field
}()

lazy var taskCreator: UIView = {
    let object = UIView()
    object.frame = CGRect(x: 10, y: 50, width: viewWidth - 20, height: viewHeight - 350)
    object.layer.cornerRadius = 10
    object.backgroundColor = UIColor.lightGray

    object.addSubview(taskNameField)
    object.addSubview(doneButton)

    return object
}()

推荐阅读