我正在尝试从UITextField
子视图之外的taskCreator
(taskNameField)子视图传递文本数据。不确定如何在子视图之外获取这些数据,以便将其添加到任务数组中。任何帮助将不胜感激。
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.
}
}
答案 0 :(得分:0)
如果要获取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
}()