Update label text in Viewcontroller from Tableview - ios

My question is similar to the one as here: Update label.text at runtime
I have a viewcontroller that contains a tableview with some textfields. When the user enters the quantity and amount in the perspective textfields I want to update a label outside the tableview after I press return on the final keyboard during at runtime
After doing some research I realize that this will get some in the textfieldDidEndEditing function of the textfield delegate in the cell itself but how will i access the label text from the viewcontroller so that i can update it? I will provide the code I have below.
import UIKit
import RealmSwift
class MaterialsCell: UITableViewCell, UITextFieldDelegate{
#IBOutlet weak var materialsDescription: UITextField!
#IBOutlet weak var materialsQuantity: UITextField!
#IBOutlet weak var materialsAmount: UITextField!
func saveMaterialsData() {
let saveMaterials = SPMaterialsRequest()
saveMaterials.setValue(self.materialsDescription!.text, forKey: "materialDescriptiopn")
saveMaterials.setValue(self.materialsQuantity!.text, forKey: "materialQuantity")
saveMaterials.setValue(self.materialsAmount!.text, forKey: "materialAmount")
let realm = try! Realm()
do {
try realm.write {
realm.add(saveMaterials)
print("added \(saveMaterials.materialDescription) to Realm Database")
print("added \(saveMaterials.materialQuantity) to Realm Database")
print("added \(saveMaterials.materialAmount) to Realm Database")
print("added \(saveMaterials.materialTotal) to Realm Database")
}
} catch {
print(error)
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
}
import UIKit
import RealmSwift
class ServiceProMaterialsController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var materialsView: UITableView!
#IBOutlet weak var materialsTotal: UILabel!
var spMaterialsRequest: Results<SPMaterialsRequest>?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "MaterialsCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MaterialsCell
else {
fatalError("Dequed Cell is not an instance of MaterialsCell")
}
cell.materialsDescription.text = ""
cell.materialsQuantity.text = ""
cell.materialsAmount.text = ""
return cell
}
}

Create a custom delegate, implement that delegate in your viewcontroller. Pass the delegate to the cell while dequeuing the cell and calling that delegate in textFieldDidEndEditing, like :
import UIKit
import RealmSwift
protocol CustomProtocol{
func textEntered(text : String,index:Int)
}
class MaterialsCell: UITableViewCell, UITextFieldDelegate{
#IBOutlet weak var materialsDescription: UITextField!
#IBOutlet weak var materialsQuantity: UITextField!
#IBOutlet weak var materialsAmount: UITextField!
var delegate : CustomProtocol?
var index : Int?
func saveMaterialsData() {
let saveMaterials = SPMaterialsRequest()
saveMaterials.setValue(self.materialsDescription!.text, forKey: "materialDescriptiopn")
saveMaterials.setValue(self.materialsQuantity!.text, forKey: "materialQuantity")
saveMaterials.setValue(self.materialsAmount!.text, forKey: "materialAmount")
let realm = try! Realm()
do {
try realm.write {
realm.add(saveMaterials)
print("added \(saveMaterials.materialDescription) to Realm Database")
print("added \(saveMaterials.materialQuantity) to Realm Database")
print("added \(saveMaterials.materialAmount) to Realm Database")
print("added \(saveMaterials.materialTotal) to Realm Database")
}
} catch {
print(error)
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func textFieldDidEndEditing(_ textField: UITextField) {
//Usage of custom protocol
delegate?.textEntered(text : textField.text!,index:self.index!)
}
}
import UIKit
import RealmSwift
class ServiceProMaterialsController: UIViewController, UITableViewDataSource, UITableViewDelegate , CustomProtocol{
#IBOutlet weak var materialsView: UITableView!
#IBOutlet weak var materialsTotal: UILabel!
var spMaterialsRequest: Results<SPMaterialsRequest>?
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "MaterialsCell"
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? MaterialsCell
else {
fatalError("Dequed Cell is not an instance of MaterialsCell")
}
//Do 1 and 2
cell.delegate = self // 1
cell.index = indexPath.row //2
cell.materialsDescription.text = ""
cell.materialsQuantity.text = ""
cell.materialsAmount.text = ""
return cell
}
//Implementing CustomProtocol
func textEntered(text : String,index:Int){
materialsTotal.text = text
}
}

Related

Cannot assign value of type 'ViewController' to type 'AddContactDelegate?'

import UIKit
struct Contact {
var fullname: String
var contactNumber: String
}
class ViewController: UITableViewController {
var contacts = [Contact]()
#IBOutlet var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func handleAdd(_ sender: Any) {
let controller = AddContacts()
controller.delegate = self
self.present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = contacts[indexPath.row].fullname
cell.detailTextLabel?.text = contacts[indexPath.row].contactNumber
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
}
import UIKit
protocol AddContactDelegate {
func addContact(contact: Contact)
}
class AddContacts: UIViewController {
var delegate: AddContactDelegate?
#IBOutlet weak var ContactTextField: UITextField!
#IBOutlet weak var nameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func save(_ sender: Any) {
guard let fullname = nameTextField.text, nameTextField.hasText else {
print("handle error here")
return
}
guard let contactNumber = ContactTextField.text , ContactTextField.hasText else {
print("enter contact error here")
return
}
let contact = Contact(fullname: fullname, contactNumber: contactNumber)
print(contact.fullname)
print(contact.contactNumber)
delegate?.addContact(contact: contact)
}
}
in viewController: UITableViewController file it shows error like Cannot assign value of type 'ViewController' to type 'AddContactDelegate?' what should do i do to solve these error
import UIKit
struct Contact {
var fullname: String
var contactNumber: String
}
class ViewController: UITableViewController {
var contacts = [Contact]()
#IBOutlet var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func handleAdd(_ sender: Any) {
let controller = AddContacts()
controller.delegate = self
self.present(UINavigationController(rootViewController: controller), animated: true, completion: nil)
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = contacts[indexPath.row].fullname
cell.detailTextLabel?.text = contacts[indexPath.row].contactNumber
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
}
// add this
extension ViewController: AddContactDelegate {
func addContact(contact: Contact) {
contacts.append(contact)
tableView.reloadData()
}
}
// thats it
import UIKit
protocol AddContactDelegate:AnyObject {
func addContact(contact: Contact)
}
class AddContacts: UIViewController {
weak var delegate: AddContactDelegate?
#IBOutlet weak var ContactTextField: UITextField!
#IBOutlet weak var nameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func save(_ sender: Any) {
guard let fullname = nameTextField.text, nameTextField.hasText else {
print("handle error here")
return
}
guard let contactNumber = ContactTextField.text , ContactTextField.hasText else {
print("enter contact error here")
return
}
let contact = Contact(fullname: fullname, contactNumber: contactNumber)
print(contact.fullname)
print(contact.contactNumber)
delegate?.addContact(contact: contact)
}
}
You must implement the protocol inside the ViewController.
Why xcode shows you the error is:
protocol ViewDelegate: AnyObject {
func didDoSomething()
}
// Which means - reference with name delegate can store objects that conform to the protocol ViewDelegate
var delegate: ViewDelegate
If you did not conform the object you are trying to store to this reference with the desired protocol, you will not be able to store that object to that reference.
You can look at protocols like contracts, if the protocol is implemented in a specific class, the class must implement the declared methods inside the protocols.
Simply implementing this protocol to your ViewController and adding the method declared in the protocol (contract) will make you achieve what you want.
class MyViewController: ViewDelegate {
func didDoSomething() {
//TODO: Logic for this method
}
}
//Will not give compile errors
let delegate: ViewDelegate = MyViewController()
Just for additional info, you can always implement a delegate in this way
class MyViewController {
//properties
}
//MARK: - ViewDelegate implementation
extension MyViewController: ViewDelegate {
func didDoSomething() {
//TODO: logic
}
}
Hope it helps.

tableview doesn't show anything?

I made sure that my cell identifier was correct I'm not too sure what the problem is. I've been rereading the code on this viewController and I'm not sure if I'm missing something or if there's a specific reason why my tableview isn't loading.
import UIKit
import JGProgressHUD
class BasketViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = footerView
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//MARK: TO DO CHECK IF USER IS LOGGED IN
loadBasketFromFirestore()
}
//MARK: VARS
var basket : basket?
var allItems : [Item] = []
var purchaseItemID : [String] = [] //holds id of items you want to purchase
var hud = JGProgressHUD(style: .dark)
//MARK: IBOUTLETS
#IBOutlet weak var totalPriceLabel: UILabel!
#IBOutlet weak var totalItemsInBasket: UILabel!
#IBOutlet weak var checkOutButton: UIButton!
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var footerView: UIView!
//MARK: IBACTIONS
#IBAction func checkOutButtonTapped(_ sender: Any) {}
//something is wrong with
//MARK: DOWNLOAD BASKET
private func loadBasketFromFirestore(){
//MARK: CHANGE 1234 TO A USER ID STRING
downloadBasketFromFirestore("1234") { (basket) in
self.basket = basket
self.getBasketItems()
}
}
private func getBasketItems(){
if (basket != nil) {
print("getting items")
downloadItems(_withIDS: basket!.itemID) { (allItems) in
self.allItems = allItems
self.tableView.reloadData()
}
} else { print("basket is nil")}
}
}
extension BasketViewController : UITableViewDataSource , UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "basketCell", for: indexPath) as! ITEMTableViewCell
cell.generateCellForITEMS(item: allItems[indexPath.row])
return cell
}
}

Is there a simple way to delete specific custom cells from a UITableView?

I am trying to instantiate empty Buyer cells (custom cell) in my table view and then have the user populate the buyers' names. When the user presses the delete button for a row/cell, it should delete the corresponding row/cell regardless of whether or not the textfield for that row has been populated or not. Clearly, I am not getting the desired behavior. For example, when I press delete Row0 (whose textfield says "Buyer 0") and the tableview reloads, Buyer 0 is still there, but one of the empty Buyer cells at the end gets deleted instead.
import UIKit
class EntryAlertViewController: UIViewController {
//Fields/Table
#IBOutlet weak var itemField: UITextField!
#IBOutlet weak var priceField: UITextField!
#IBOutlet weak var tableView: UITableView!
//Visual Components
#IBOutlet weak var mainView: UIView!
#IBOutlet weak var titleView: UIView!
#IBOutlet weak var splitItemButton: UIButton!
#IBOutlet weak var cancelButton: UIButton!
#IBOutlet weak var addItemButton: UIButton!
//Commonly Used Objects/Variables
var potentialBuyers: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
potentialBuyers.append("")
tableView.dataSource = self
tableView.register(UINib(nibName: "BuyerCell", bundle: nil), forCellReuseIdentifier: "ReusableCell")
}
override func viewWillAppear(_ animated: Bool) {
}
#IBAction func splitItemPressed(_ sender: UIButton) {
potentialBuyers.append("")
tableView.reloadData()
}
}
Here are the tableview datasource and the delete button delegate.
extension EntryAlertViewController: UITableViewDataSource, DeleteButtonDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return potentialBuyers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! BuyerCell
cell.deleteButtonDelegate = self
cell.indexPath = indexPath
cell.nameField.text = cell.buyerName
if potentialBuyers.count == 1 {
cell.deleteButton.isHidden = true
} else {
cell.deleteButton.isHidden = false
}
return cell
}
func deletePressed(index: Int) {
potentialBuyers.remove(at: index)
tableView.reloadData()
}
}
And here is my BuyerCell class with the UITextFieldDelegate as an extension.
import UIKit
protocol DeleteButtonDelegate {
func deletePressed(index: Int)
}
class BuyerCell: UITableViewCell {
#IBOutlet weak var deleteButton: UIButton!
#IBOutlet weak var nameField: UITextField!
var deleteButtonDelegate: DeleteButtonDelegate!
var indexPath: IndexPath!
var buyerName: String?
override func awakeFromNib() {
super.awakeFromNib()
self.nameField.delegate = self
}
#IBAction func deletePressed(_ sender: UIButton) {
//print the indexPath.row that this was pressed for
print("delet pressed for \(indexPath.row)")
self.deleteButtonDelegate?.deletePressed(index: indexPath.row)
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
extension BuyerCell: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
print("textFieldDidBeginEditing")
buyerName = nameField.text
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing")
buyerName = nameField.text
}
}
Your problem is in this line
cell.nameField.text = cell.buyerName
Cells are reused from a reuse pool, so you can't rely on the cell holding any particular state or value.
Your buyer name needs to come from your data model array.
Something like
cell.nameField.text = self.potentialBuyers[indexPath.row]
Reloading the whole tableview is a bit excessive when you have only deleted a single row; Just delete the relevant row.
You can also clean up your delegation protocol so that there is no need for the cell to track its indexPath -
protocol DeleteButtonDelegate {
func deletePressed(in cell: UITableViewCell)
}
In your cell:
#IBAction func deletePressed(_ sender: UIButton) {
self.deleteButtonDelegate?.deletePressed(in: self)
}
In your view controller:
func deletePressed(in cell: UITableViewCell) {
guard let indexPath = tableView.indexPath(for: cell) else {
return
}
potentialBuyers.remove(at: indexPath.row)
tableView.deleteRows(at:[indexPath], with: .automatic)
}
There is a major issue in your code. You are not updating the data model so the changes in the cells are lost when the user scrolls.
Rather then quite objective-c-ish protocol/delegate in Swift callback closures are much more convenient and efficient. You can use one callback for both updating the model and deleting the cell.
Replace the BuyerCell cell with
class BuyerCell: UITableViewCell {
#IBOutlet weak var deleteButton: UIButton!
#IBOutlet weak var nameField: UITextField!
var callback : ((UITableViewCell, String?) -> Void)?
override func awakeFromNib() {
super.awakeFromNib()
self.nameField.delegate = self
}
#IBAction func deletePressed(_ sender: UIButton) {
callback?(self, nil)
}
}
extension BuyerCell: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
print("textFieldDidBeginEditing")
callback?(self, nameField.text)
}
func textFieldDidEndEditing(_ textField: UITextField) {
print("textFieldDidEndEditing")
callback?(self, nameField.text)
}
}
In the controller in cellForRow assign the callback and handle the actions. The actions work also reliably if cells are reordered, inserted or deleted.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReusableCell", for: indexPath) as! BuyerCell
let buyerName = potentialBuyers[indexPath.row]
cell.nameField.text = buyerName
cell.callback = { [unowned self] cCell, cName in
let currentIndexPath = tableView.indexPath(for: cCell)!
if let name = cName {
self.potentialBuyers[currentIndexPath.row] = name
} else {
self.potentialBuyers.remove(at: currentIndexPath.row)
tableView.deleteRows(at: [currentIndexPath], with: .fade)
}
}
cell.deleteButton.isHidden = potentialBuyers.count == 1
return cell
}

Updating Label in Cell

I have a TableView which rows contain label and two buttons. What I wanna do is that when a user clicks the first button "Set Name", a pop up view comes up in which he can input text from keyboard. After hitting "Set", pop up view is dismissed and label inside a row containing the clicked button changes to the input text. I set the delegates but I cannot make label to change.
TableView:
import UIKit
class SetGame: UIViewController, UITableViewDelegate, UITableViewDataSource
{
var numOfPlayers = Int()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return numOfPlayers
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
cell.Name.text = "Player \(indexPath.row + 1)"
cell.btn1.tag = indexPath.row
cell.btn2.tag = indexPath.row
return cell
}
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func setName(sender: UIButton)
{
let thisVC = storyboard?.instantiateViewController(withIdentifier: "SetName") as! SetName
thisVC.delegate = self
present(thisVC, animated: true, completion: nil)
}
#IBAction func setFingerprint(_ sender: UIButton)
{
}
#IBAction func unwindToSetGame(_ segue: UIStoryboardSegue)
{
print("unwinded to SetGame")
}
#IBOutlet weak var tableView: UITableView!
}
extension SetGame: nameDelegate
{
func named(name: String)
{
let indexP = IndexPath(row: 0, section: 0)
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell
cell.Name.text = "bkjhvghcjhkv"
//wanted to see if it changes first cell. But doesn't work
}
}
TableViewCell Class:
import UIKit
class TableViewCell: UITableViewCell
{
override func awakeFromNib()
{
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
#IBOutlet weak var Name: UILabel!
#IBOutlet weak var btn1: UIButton!
#IBOutlet weak var btn2: UIButton!
}
Pop up View:
import UIKit
protocol nameDelegate
{
func named(name: String)
}
class SetName: UIViewController
{
var delegate: nameDelegate!
override func viewDidLoad()
{
super.viewDidLoad()
window.layer.borderWidth = 1
window.layer.borderColor = UIColor.white.cgColor
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
#IBAction func closePopUp(_ sender: Any)
{
if input.text != ""
{
delegate.named(name: input.text!)
}
dismiss(animated: true, completion: nil)
}
#IBOutlet weak var input: UITextField!
#IBOutlet weak var window: UIView!
}
Replace this
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell
with
let cell = tableView.cellForRow(at:indexP) as! TableViewCell

How to pass TableViewCell value into new ViewController in Swift 3.0?

I have this JSON data
move.json
{
"status":"ok",
"movement":
[
{
"refno":"REF 1",
"dtfrom":"2017-13-12"
},
{
"refno":"REF 2",
"dtfrom":"2017-13-13"
},
{
"refno":"REF 3",
"dtfrom":"2017-13-14"
},
]
}
So far, I managed to fetch the value into TableViewCell.
But my goal is to pass the value from ViewController.swift into MoveDetails.swift so the value can be display in MoveDetails.swift
And I have these four swift files. I'm having the problem on ViewController.swift and MoveDetails.swift. I'm not sure how to pass the value into new Controller.
The code as below.
ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableview: UITableView!
var move: [Move]? = []
override func viewDidLoad() {
super.viewDidLoad()
fetchData()
}
func fetchData() {
let urlRequest = URLRequest(url: URL(string: "http://localhost/move.json")!)
let task = URLSession.shared.dataTask(with: urlRequest) {
(data,response,error)in
if error != nil { return }
self.move = [Move]()
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [String: AnyObject]
if let msFromJson = json["movement"] as? [[String: AnyObject]] {
for mFromJson in msFromJson {
let ms = Move()
if let refno = mFromJson["refno"] as? String, let dtfrom = mFromJson["dtfrom"] as? String {
ms.refno = refno
ms.dtfrom = dtfrom
}
self.move?.append(ms)
}
}
DispatchQueue.main.async {
self.tableview.reloadData()
}
}
catch let error{ print(error)}
}
task.resume()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "movementstatusCell", for: indexPath) as! MoveCell
cell.refnoLbl.text = self.move?[indexPath.item].refno
cell.dtfromLbl.text = self.move?[indexPath.item].dtfrom
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.move?.count ?? 0
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MoveDetails") as! MoveDetails
let selectedMove = self.move?[indexPath.item]
vc.refnoString = selectedMove.refno
vc.dtfromString= selectedMove.dtfrom
self.navigationController?.pushViewController(vc, animated: true)
}
}
MoveCell.swift
import UIKit
class MoveCell: UITableViewCell {
#IBOutlet weak var dtfromLbl: UILabel!
#IBOutlet weak var refnoLbl: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Move.swift (NSObject)
import UIKit
class Move: NSObject {
var refno: String?
var dtfrom: String?
}
MoveDetails.swift
import UIKit
class MoveDetails: UIViewController {
#IBOutlet weak var refnoLbl: UILabel!
#IBOutlet weak var dtfromLbl: UILabel!
var refnoString: String!
var dtfromString: String!
override func viewDidLoad() {
super.viewDidLoad()
refnoString = refnoLbl.text
dtfromString = dtfromLbl.text
}
override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }
}
Appreciate if someone can help. Thanks.
You will just have to set the properties of your MoveDetails view controller. And as a suggestion
Instead of storing refnoString and dtfromString properties in MoveDetails, you could just store one property of type Move:
Cache MoveDetails view controller to reuse it
Implement viewDidAppear to update the MoveDetails outlets
So:
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var detailsVC : MoveDetails?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (detailsVC == nil) {
detailsVC = self.storyboard?.instantiateViewController(withIdentifier: "MoveDetails") as! MoveDetails
}
detailsVC.move = self.move?[indexPath.item]
self.navigationController?.pushViewController(detailsVC , animated: true)
}
}
Then, override viewDidAppear in MoveDetails view controller and there you just fill in the values into the text label outlets.
class MoveDetails: UIViewController {
#IBOutlet weak var refnoLbl: UILabel!
#IBOutlet weak var dtfromLbl: UILabel!
var move:Move?
override func func viewDidAppear(_ animated: Bool) {
refnoLbl.text = move?.refno
dtfromLbl.text = move?.dtfrom
}
}
Syntax errors cause because I currently have no Xcode available to do the checking

Resources