How do I get indirect binding to work?

  • In “Cocoa Bindings Programming Topics “ from ADC, on page 18 there is a Figure 6 called “Typical bindings configuration using existing controller”. (In figure 6 there is MyDocument between  NSObjectController and the model object). A quote from the text

    “Although the NSController instance is conceptually bound directly to its model object, in most situations the binding will be “indirect,” to a variable in your document object, as shown in Figure 6.”

    I’m trying to figure out how to get this “indirect binding” to work.

    First I created a simple app consisting of a single text field which I bound to a model called title. The model title is a instance variable belonging to a class called Book. Here’s how my Book class looks like:

    @interface Book : NSObject
    {
        NSString *title;
    }
    -(NSString *) title;
    -(void)setTitle:(NSString *) title;
    @end

    In the IB I have essentially 4 objects
    1. a text field (NSTextField)
    2. NSObjectController
    3. an instance of a Book.
    4. File’s Owner (which I don’t use yet)

    Here are the connections and choices I made in IB.

    NSObjectController Inspector
        Connections
            Outlets/content:Book
        Attributes
            Mode: Class
            Object Class Name: NSMutableDictionary
            Keys:title

    NSTextField Inspector
        Bindings
            Bind to: NSObjectController
            Controller Key: selection
            Model Key Path: title

    This app works. But my question is how to modify this app so that I introduce MyDocument between the NSObjectController and the Book instance?

    I suspect that
    1. NSObjectController content should point at MyDocument
    2. MyDocument should have an outlet (which I called book), pointing at my Book object.
    3. NSTextField Inspector Model Key Path: should be changed from title to book.title

    Is this correct? I tried this but my app keeps crashing on quit from app.

    Bob


    ____________________________________________________________________________________
    Pinpoint customers who are looking for what you sell.
    http://searchmarketing.yahoo.com/
  • on 10/5/07 11:28 AM, <bobueland...> purportedly said:

    > 1. NSObjectController content should point at MyDocument

    Or you could use the content binding. Depends on what the content really
    is--is it the document object, or the book object?

    > 2. MyDocument should have an outlet (which I called book), pointing at my Book
    > object.

    Generally, you wouldn't want to instantiate your model object in your nib.
    It would be best if the document object "owns" the book object.

    > 3. NSTextField Inspector Model Key Path: should be changed from title to
    > book.title

    Yes, considering that your document object is the controller's content and
    "book" is the property for the book object in the document object.

    > Is this correct? I tried this but my app keeps crashing on quit from app.

    It should work, although it may not be entirely "correct" by various
    reckoning. Why your app is crashing, is anyone's guess, since you haven't
    indicated the "why" of the crash--i.e. the error being reported, rather than
    what is in your code that is causing it, such as "EXEC BAD ACCESS".

    Are you, by chance, explicitly releasing the "book" object in the document
    object somewhere, without having explicitly retained it beforehand?

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
  • Keary Suska wrote

    why your app is crashing, is anyone's guess, since you haven't

    indicated the "why" of the crash--i.e. the error being reported,...

    Are you, by chance, explicitly releasing the "book" object in the document

    object somewhere, without having explicitly retained it beforehand?

    This is the error message I get in the log window

    "An instance 0x34a970 of class Book is
    being deallocated while key value observers are still registered with
    it.  Break on _NSKVODeallocateLog to start debugging."

    This is how my Book.m file looks like

    #import "Book.h"

    @implementation Book

    // getters and setters

    -(NSString *) title

    {

        return title;

    }

    -(void)setTitle:(NSString *) titleVal

    {

        titleVal=[titleVal copy];

        [title release];

        title=titleVal;

        NSLog(@"%@", title);

    }

    // init and dealloc

    -(id) init

    {

        [super init];

        [self setTitle:@"New Title"];

        return self;

    }

    -(void) dealloc

    {

        [title release];

        [super dealloc];

    }

    @end

    Has the problem something to do with  [title release]; since title is
    under kvo. The crash happens when I choose "Quit New Application". But
    I still don't understand why.

    Bob


    ____________________________________________________________________________________
    Be a better Heartthrob. Get better relationship answers from someone who knows. Yahoo! Answers - Check it out.
    http://answers.yahoo.com/dir/?link=list&sid=396545433
  • Keary Suska wrote
    Generally, you wouldn't want to instantiate your model object in your nib.
    It would be best if the document object "owns" the book object.

    I took away the book instance in the nib and the outlet from MyDocument pointing to the book instance. Instead I made an instance variable in MyDocument.h like this

            Book *book;

    I then changed the code so that the document owns the book object by adding this line to the init of MyDocument-m

            book=[[Book alloc] init];

    Now everything works and no more crashes, but still not wiser why.

    Bob

          ____________________________________________________________________________________
    Fussy? Opinionated? Impossible to please? Perfect.  Join Yahoo!'s user panel and lay it on us. http://surveylink.yahoo.com/gmrs/yahoo_panel_invite.asp?a=7
  • on 10/5/07 3:38 PM, <bobueland...> purportedly said:

    > This is the error message I get in the log window
    >
    > "An instance 0x34a970 of class Book is
    > being deallocated while key value observers are still registered with
    > it.  Break on _NSKVODeallocateLog to start debugging."
    >
    > Has the problem something to do with  [title release]; since title is
    > under kvo. The crash happens when I choose "Quit New Application". But
    > I still don't understand why.

    Probably not. The message says that the issue is with a "book" object. The
    relevant code is probably in the document class, or any other class that may
    be allocating/deallocating a book object. Objects are not normally
    deallocated when an application quits, so this message is odd.

    Usually the message means that you have called -addObserver: on an object
    but didn't call -removeObserver: before deallocating the observed object.
    This doesn't happen as often with IB set bindings, sine they tend to take
    care of themselves, although it will happen in certain circumstances when
    manually loading nibs. In this case, there is usually a circular reference
    preventing either the proper automatic unbinding or objects getting
    deallocated in the order that you want.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today