I'm trying to pass the text data from my UITextField
(taskNameField) subview that's apart of my taskCreator
subview. Not sure how to get my hands on this data outside of the subview so that I can add it to my task array. Any help would be greatly appreciated.
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.
}
}
If you want to get the value of a UITextField
it is as simple as this.
var textFieldValue = taskNameField.text
However if you need it immediately after the user has entered something into the text field, then you need to use delegate methods available in the UITextFieldDelegate
which your UIViewController
has to conform to.
func textFieldDidEndEditing(textField: UITextField) {
var textFieldValue = textField.text
// Do something with the value like adding it to a Task object
}
If you need to access your text field outside your view, then you need to declare it outside. Also declare them as lazy
so that they are created only when needed.
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
}()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.