Access an array of objects stored in one view controller from a different vc

Discussion in 'Mac Programming' started by isthisonetaken, Mar 23, 2011.

  1. isthisonetaken macrumors regular

    Jun 29, 2006
    Hey All,

    so here is a breakdown of my app, it is a client server app used for auctions. The server holds an array of items that are up for auction, the clients (bidders) will get those items, view them and bid on them. I'm currently working on being able to add, delete and modify the auction items in the Auction House app. I should note I'm new to using MVC and am trying to implement it as much, and as correct as possible. Here is a breakdown of my files:

    Model Classes
    Item -> holds the information representing an auction item (name, description, pic, ect)
    AuctionHouse -> holds information for the auction (connected bidders, if the auction is running and an array of items)

    View Classes
    MainMenu.xib -> is the main window, allows the user to start/ stop the auction and has a button which loads the other window for managing auction items
    AuctionItems.xib -> has a table view for displaying all the auction items, loads a sheet for adding/ modifying auction items

    Controller Classes
    AuctionHouseViewController -> handles most of the functionality of the app in terms of starting/ stoping the auction and interacting with bidders
    AuctionItemsController -> has the outlets for the view items, actions for adding, removing, modifying etc and I think it should also have the table view methods.

    My AuctionHouseViewController creates an AuctionHouse item and uses it throughout the lifecycle of the application. I'm stuck now in how do I access the array of auction items stored in that object in the AuctionItemsController files?

    Any suggestions?
  2. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Post the @interface for AuctionHouse.

    Frankly, I expected to see an Auction model class. This may be my ignorance, but I thought a real-world auction house could have multiple auctions. For example, different ones scheduled on different days, or an art auction on the same day as a furniture auction.

    An Auction class would answer the question of how to access the array of AuctionItems: an Auction instance is the array of AuctionItems.

    You can have any number of views or controllers use the same Auction instance, which answers the question of how to use an array stored in one controller from another controller: both controllers are created so they reference the same Auction instance.
  3. isthisonetaken thread starter macrumors regular

    Jun 29, 2006
    Here is what I have now (includes code for turning AuctionHouse into a singleton). Before, it didn't have the #import "SynthesizeSingleton.h" and + (AuctionHouse *)sharedAuctionHouse;

    #import <Foundation/Foundation.h>
    #import "GCDAsyncSocket.h"
    #import "Item.h"
    #import "SynthesizeSingleton.h"
    @interface AuctionHouse : NSObject {
        BOOL isRunning;
        GCDAsyncSocket *socket;
        NSMutableDictionary *connectedBidders;
        dispatch_queue_t socketQueue;
        NSMutableArray *auctionItems;
    @property BOOL isRunning;
    @property (retain) GCDAsyncSocket *socket;
    @property (retain) NSMutableDictionary *connectedBidders;
    @property dispatch_queue_t socketQueue;
    @property (retain) NSMutableArray *auctionItems;
    + (AuctionHouse *)sharedAuctionHouse;
    I did some more google searching and found this:

    I followed what it said and turned my AuctionHouse into a singleton, changed all my code to now call [AuctionHouse sharedAuctionHouse] instead of auction. I just tested it and it works properly.

    I only need one auction running at a time, this isn't going to be a real world auction application.

    Where would I instantiate the Auction item? Before doing it with singletons, I was instantiating an AuctionHouse object as auction in my AuctionHouseViewController and I didn't know how to access that object in the AuctionItems. I thought maybe of doing it in the AppDelegate, and in searching for that, I found the link above.
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    I would argue you've misnamed the class AuctionHouse. It should be named Auction.

    Example: Sotheby's and Christie's are auction houses (for art). They aren't auctions, in and of themselves. They perform (provide or supply) auctions (activities involving selected sets of auctionable items) as one of their engageable activities. Bidders engage in an auction (a specific identifiable nexus of activity); they don't engage in an auction house (a collection of auctions and other activities, such as payouts to the people bringing auctionable items to the house).

    Then AuctionHouseViewController becomes AuctionViewController, which makes more sense given its description of "starting/ stoping the auction and interacting with bidders".

    I also don't think you should expose a mutable array of auction Items. It gives uncontrolled access to the list, which means you can't notify controllers or views when something adds or removes an item.

    I think you should have the auction items exposed only as a NSTableView data source. Then your AuctionItemsController should be an AuctionItemsView, which is a container composed of the NSTableView that shows auction items, plus the controls for adding and removing auction items. I don't see why you'd need a separate controller class there, when a data source suffices.

    I also think you shouldn't expose details of how bidders are connected or grouped into collections. That suggests modeling Bidder, which is then connected or subscribed to an Auction.

    Since you said you're only doing one auction, that suggests that Auction is a singleton, which means any view or controller can gain access to it.

    If you had multiple Auctions active at once, then you'd create an Auction instance first, and set it up. Then you'd create view and controller instances that you init'ed with that Auction. This binds together all the parts, centered on the model object: a single Auction.

    There is nothing inherent to an auction that suggests it needs to be a singleton. I think you're using singleton as a crutch because you don't yet have an Auction-centered design. By making it a singleton it lets you avoid the problem of how to tell the controller(s) and view(s) which Auction object to use. Unfortunately, overuse of singletons is difficult to change later, in the same way that use of global variables in programs is difficult to change later. Too much implied context.

    It doesn't matter if it's a real-world auction or not. The important part is the relationships and roles, and that's the same no matter where the auction occurs. The reason for using MVC is to be able to make a Model that accurately provides the necessary roles and relationships. The Views and Controllers then operate on the Model. If the Model is inaccurate, or the placement of responsibilities is wrong, e.g. in a controller instead of in the model, then the design itself often becomes harder to work with. So getting an accurate model is an important part of getting view and controller classes.
  5. isthisonetaken thread starter macrumors regular

    Jun 29, 2006
    How do I expose the items only as a NSTableView data source? Right now, I have AuctionItems.xib which has a NSTableView and buttons to add/ remove/ modify an item. Clicking on add or modify will load a sheet which allows the user to enter the details for the new (or modified) item. AuctionItemsController than handles the IBActions and as of now I'm using AuctionHouse as a singleton. So would I still have the .xib and rename AuctionItemsController to AuctionItemsView but keep the same code for handling the buttons?

    Do you mean the connectedBidders dictionary? I do have a Bidders model which is used in the bidding client app. That dictionary is holding the sockets created by GCDAsyncSocket so I can talk to the bidding clients individually as needed.

    I don't think the singleton approach is working anyways, as it keeps crashing when I try to add a new item. I think I should go back and redo so it's an Auction centred app like you are suggesting. So let me see if I understand what you are saying (minus the questions I listed above):

    Auction.h/m -> holds the same information as AuctionHouse is right now
    Item.h/m -> doesn't change

    MainMenu.xib -> doesn't change
    AuctionItems.xib -> doesn't change
    AuctionItemsView.h/m -> handles the IBActions for the add/remove/modify buttons (and becomes the tableview data source???)

    AuctionHouseAppDelegate -> doesn't change (I only added in shouldTerminateAfterLastWindowClosed)
    AuctionViewController.h/m -> takes all the same code as AuctionHouseViewController has right now

    I understand about the renaming, but I'm still confused about how AuctionItemsView is going to get the data stored in Auction into its tableview.

Share This Page