Pass the received strings from another table controller

I am building a quite simple product ordering app. This is a tabbed application being, Product List, Order List and settings. From the Product List (coredata parsed from remote csv file) I can search then select a row, which then pops an alert view to add desired quantity to textfield and then on “ADD” button in alertview I can segue with the four strings (Productcode, Detail, Quantity(from alertview) and barcode) to the Order List View controller. This is where im having trouble! I can print the four strings in console at the point of being recieved by OrderList, but I’m stuck for a way to pass those strings into My NSObject model in order to display and persist them in the labels of the tableviewcell.

Here is my OrderProduct.swift file:

import Foundation
import UIKit

class OrderProduct: NSObject, NSCoding {

var uuid: String = NSUUID().UUIDString
var productcode:String = ""
var detail:String = ""
var quantity:String = ""
var barcode:String = ""
var inOrderList = false

func encodeWithCoder(coder: NSCoder) {
    coder.encodeObject(uuid, forKey: "uuid")
    coder.encodeObject(productcode, forKey: "productcode")
    coder.encodeObject(detail, forKey: "detail")
    coder.encodeObject(quantity, forKey: "quantity")
    coder.encodeObject(barcode, forKey: "barcode")
    coder.encodeBool(inOrderList, forKey: "inOrderList")
required init?(coder decoder: NSCoder) {

    if let archivedUuid = decoder.decodeObjectForKey("uuid") as? String {
        uuid = archivedUuid

    if let archivedProductcode = decoder.decodeObjectForKey("productcode") as? String {
        productcode = archivedProductcode

    if let archivedDetail = decoder.decodeObjectForKey("detail") as? String {
        detail = archivedDetail

    if let archivedQuantity = decoder.decodeObjectForKey("quantity") as? String {
        quantity = archivedQuantity

    if let archivedBarcode = decoder.decodeObjectForKey("barcode") as? String {
        barcode = archivedBarcode

    inOrderList = decoder.decodeBoolForKey("inOrderList")
init(productcode: String, detail: String, quantity: String, barcode: String) {

    self.productcode = productcode
    self.detail = detail
    self.quantity = quantity
    self.barcode = barcode

And here is my OrderViewController.swift file:

import Foundation
import UIKit

//MARK: - Class
class TestOrderViewController: UITableViewController{

required init?(coder decoder: NSCoder) {
    //print("init OrderViewController")
    super.init(coder: decoder)


    deinit {

//MARK: - Product Vars
    var passedProductcode = String()     //String is recieved here!
    var passedDetail = String()          //String is recieved here!
    var passedQuantity = String()        //String is recieved here!
    var passedBarcode = String()         //String is recieved here!

    var orderProducts = [OrderProduct]() //My Object model...

//MARK: - ViewDidLoad
override func viewDidLoad() {
    title = "Order List"

    orderProducts.productcode = passedProductcode //Trying to pass them but
    orderProducts.detail = passedDetail           //errors with "Value of type
    orderProducts.quantity = passedQuantity       //'[OrderProduct]' has no
    orderProducts.barcode = passedBarcode         //member 'productcode'?

    tableView.estimatedRowHeight = 80.0
    tableView.rowHeight = UITableViewAutomaticDimension

// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // Return the number of sections.
    return 1

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // Return the number of rows in the section.

    return orderProducts.count

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("OrderCell", forIndexPath: indexPath) as! OrderTableViewCell
    let orderProduct = orderProducts[indexPath.row]
    cell.productcodeLabel.text = orderProduct.productcode
    cell.detailLabel.text = orderProduct.detail
    cell.quantityLabel.text = "X (orderProduct.quantity)"
    cell.barcodeLabel.text = orderProduct.barcode

    return cell

private func loadOrderProducts() {
    if let filePath = pathForOrderProducts() where NSFileManager.defaultManager().fileExistsAtPath(filePath) {
        if let archivedOrderProducts = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? [OrderProduct] {
            orderProducts = archivedOrderProducts

private func pathForOrderProducts() -> String? {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)

    if let documents = paths.first, let documentsURL = NSURL(string: documents) {
        return documentsURL.URLByAppendingPathComponent("orderProducts.plist").path
    return nil

private func saveOrderProducts() {
    if let filePath = pathForOrderProducts() {
        NSKeyedArchiver.archiveRootObject(orderProducts, toFile: filePath)

Could somebody please guide me to what I am missing?

Your orderProducts
property in TestOrderViewController
is an Array of OrderProduct
so, when you try to do

cell.productcodeLabel.text = orderProducts.productcode //passedProductcode
cell.detailLabel.text = orderProducts.detail //passedDetail
cell.quantityLabel.text = "X (orderProducts.quantity/*passedQuantity*/)"
cell.barcodeLabel.text = orderProducts.barcode //passedBarcode

You’re attempting to retrieve property-values on the array of OrderProduct
rather than the instances inside it. It should probably

cell.productcodeLabel.text = orderProducts[indexPath.row].productcode //passedProductcode
cell.detailLabel.text = orderProducts[indexPath.row].detail //passedDetail
cell.quantityLabel.text = "X (orderProducts.quantity/*passedQuantity*/)"
cell.barcodeLabel.text = orderProducts[indexPath.row].barcode //passedBarcode

But you need to make sure your array contains the proper models (obviously).

OK, as promised I’ll try to make a more complete example. This probably won’t match your requirements 100% (which are note entirely clear to me anyway) but hopefully It’ll get you on the way…

In the viewController where you are actually passing the values I would suggest that you rather pass an array of model-objects. This could be something like this:

struct ProductModel {
    var productcode: String
    var detail: String
    var quantity: String
    var barcode: String

var models = [ProductModel]()

So you’ll crete instances of ProductModel and append these to the array. When you’re ready to move to the next viewController TestOrderViewController
by the looks of it you’ll pass it the whole array (as it will have a matching property). This will either be done before you push the viewController, or in prepareForSegue based on how you handle the viewControllers.

Then in TestOrderViewController
you will have something like the below:

// UITableViewDataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return models.count

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("OrderCell", forIndexPath: indexPath) as! OrderTableViewCell
    let model = models[indexPath.row]
    cell.productcodeLabel.text = model.productcode
    cell.detailLabel.text = model.detail
    cell.quantityLabel.text = "X (model.quantity)"
    cell.barcodeLabel.text = model.barcode
    return cell
Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Pass the received strings from another table controller

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录