0

I have a table view controller named ThirdViewController which displays a list of transactions. When a user presses the add button, the app segues to the FifthViewController where they can enter the transaction name, value and date. I am trying to get this data entered in FifthViewController into a class named ArrayData so that I can add it to the array which will then add the payment to the ThirdViewController table view.

This is my code for the ThirdViewController.swift:

var arrayObject = ArrayData()

class ThirdViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// #pragma mark - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
    // #warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1
}

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return arrayObject.paymentsArray().count
}


override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    var cell:CustomTransactionTableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as CustomTransactionTableViewCell
    cell.paymentNameLabel.text = (arrayObject.paymentsArray().objectAtIndex(indexPath.row)) as String
    cell.costLabel.text = (arrayObject.costArray().objectAtIndex(indexPath.row)) as String
    cell.dateLabel.text = (arrayObject.dateArray().objectAtIndex(indexPath.row)) as String
    return cell
}
}

This is the code for arrayData.swift:

class ArrayData: NSObject {

func paymentsArray() -> NSMutableArray {
    var arrayDataPayments: NSMutableArray = ["Test"]
    return arrayDataPayments
}

func costArray() -> NSMutableArray {
    var arrayDataCost: NSMutableArray = ["£100"]
    return arrayDataCost
}

func dateArray() -> NSMutableArray {
    var arrayDataDate: NSMutableArray = ["12/07/14"]
    return arrayDataDate
}

}

And this is the code for FifthViewController:

class FifthViewController: UIViewController {

@IBOutlet var transactionNameInput : UITextField
@IBOutlet var transactionDateInput : UITextField
@IBOutlet var transactionValueInput : UITextField
@IBOutlet var addBackButton : UIButton

@IBAction func addTransactionButton(sender : AnyObject) {
    self.navigationController.popToRootViewControllerAnimated(true)

    var transactionName = transactionNameInput.text
    var transactionDate = transactionDateInput.text
    var transactionValue = transactionValueInput.text
}

@IBAction func viewTapped(sender : AnyObject) {
    //Closes keyboard when user touches screen

    transactionDateInput.resignFirstResponder()
    transactionNameInput.resignFirstResponder()
    transactionValueInput.resignFirstResponder()
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
}

How can I get the variables from the FifthViewController to the ArrayData class so they can be added to the arrays? This might be very simple but I'm new to Xcode and obviously Swift.

Any help would be greatly appreciated.

1 Answer 1

1

The methods in your ArrayData class are awkwardly implemented as each invocation returns a new array. Perhaps this is what you really want, but it is unusual (and you've noted that you are new to Xcode and Swift and thus I suspect it isn't really what you want). A more normal implementation would be :

class ArrayData: NSObject {
  var arrayDataPayments: NSMutableArray = ["Test"]
  var arrayDataCost: NSMutableArray = ["£100"]
  var arrayDataDate: NSMutableArray = ["12/07/14"]

  func paymentsArray() -> NSMutableArray { return arrayDataPayments }

  func costArray() -> NSMutableArray { return arrayDataCost }

  func dateArray() -> NSMutableArray { return arrayDataDate }
}

Now, I'm not saying that this is going to solve your bigger issue of getting the data in an out of your controller; but, I suspect that the above is a better start.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the improvement! The way I had it implemented before was from a tutorial I followed, but I can see why your way is better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.