-2

I'm trying to build a master / detail view with data stored in Core Data. Which complicates things is that the shown values are not necessarily the raw values of the properties, but might need additional calculations.

Core Data Entities

Transaction

  • amount (always a positive value)
  • fromAccount: source account for the money
  • toAccount: destination account for the money

Master view

The master view is a table view listing all the objects of the entity Transaction for a given Account (let's call it the represented account). This entails all transactions where the represented account is either the fromAccount or the toAccount.

The displayed data is:

  • simple properties such as transaction date and message

  • properties depending on the perspective: amount and account

    Transaction viewed from the perspective of the fromAccount:

    • amount: indicates a subtracted amount and is shown as (amount * -1)
    • account: show the toAccount

    Transaction viewed from the perspective of the toAccount:

    • amount: indicates an added amount and is shown as the positive value amount
    • account: show the fromAccount

The translation / interpretation of these dependent properties is done by the table view's data source. The view knows which Account it represents, so the data source can easily do the necessary calculations.

Detail view

The detail view shows the values for a single Transaction selected in the master view.

The selected object in the table view is a descendant of NSManagedObject. Ideally I would bind the values of text fields / popup menus to the properties of the Transaction. Due to the necessary interpretations this is impossible.

Right now I'm resorting to binding the values to methods such as viewController.selectedObjectAmount and viewController.selectedObjectAccount, which do the calculations. However, this seems like a lousy solution. Either I add a method for every property I want to use in my detail view, or I have a mix of methods in viewController and properties straight from the selected object itself.

My initial thought was using a "TransactionProxy" (descendant of NSProxy) which would have properties / methods for all the required properties I want to show. I gave this up as I couldn't get to work, but I'm still wondering whether it's an / the option.

1
  • 2
    just a variation of the millions "pass data/object/array to view controller" question. Who wants to select a duplicate? Jul 13, 2014 at 10:12

1 Answer 1

0

Just give the detail view a property of type Transaction and populate the view with the data from that object. It's a very common pattern.

4
  • I don't think I understand it fully. If I do that, would I not still have to bind to Transaction.amount, keeping the issue?
    – nephilim
    Jul 15, 2014 at 20:01
  • What do you mean, "bind"? Just access or change transaction.amount. Just put transaction.fromAccount.name into a label. What is so confusing?
    – Mundi
    Jul 15, 2014 at 21:18
  • I'd like to bind my textfields etc in IB to my properties. The difficulty being exactly that I can't just bind to amount, but that I need a calculation to be done (use either amount or (amount * -1). The same goes for the accounts. I need to show either fromAccount or toAccount, which is easily solvable in my table, using its datasource, but I'm at a loss when it comes to a simple detail view with a bunch of fields.
    – nephilim
    Jul 16, 2014 at 3:37
  • You are confusing "properties" of controller classes that represent UI elements (such as a label) which you create in IB, with "attributes" of the managed object which represent the data.
    – Mundi
    Jul 16, 2014 at 7:47

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.