如何使用Swift 4将数据从Pop-Over子视图传递到View Controller
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用Swift 4将数据从Pop-Over子视图传递到View Controller相关的知识,希望对你有一定的参考价值。
我正在尝试从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.
}
}
答案
如果你想获得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
}()
以上是关于如何使用Swift 4将数据从Pop-Over子视图传递到View Controller的主要内容,如果未能解决你的问题,请参考以下文章
如何在 swift 4 中使用 JSONDecoder 从嵌套 JSON 中获取数据?
swift 4 中使用 performSegue 打开的关闭页面时,如何在视图控制器和 TableViewController 之间传递数据?