How does a custom view update a model object?

This is a Cocoa n00b question - I have been programming GUI applications for years in other environments, but now I would like to understand what the "idiomatic Cocoa" is for the following trivialized situation:

I have a simple custom NSViewone that allows the user to draw simple shapes in it. Its implementation drawRectis as follows:

- (void)drawRect:(NSRect)rect
{
    // Draw a white background.
    [[NSColor whiteColor] set];
    NSRect bounds = [self bounds];
    [NSBezierPath fillRect:bounds];

    [[NSColor blackColor] set];

    // 'shapes' is a NSMutableArray instance variable
    // whose elements are NSValues, each wrapping an NSRect.
    for (NSValue *value in shapes)
    {
        NSRect someRect;
        [value getValue:&someRect];
        [self drawShapeForRect:someRect];
    }

    // In addition to drawing the shapes in the 'shapes'
    // array, we draw the shape based on the user's
    // current drag interaction.
    [self drawShapeForRect:[self dragRect]];
}

You can see how simple this code is: an array instance variable shapesacts like the model the method drawRectuses to draw shapes. New ones NSRectare added to shapesevery time the user executes the mouse / drag / mouse sequence, which I also implemented in this user view. Here is my question:

"" Cocoa, ?

, ? , NSMutableArray, , , API. , , / , , . GUI , - , , - , - .

, Cocoa delegate , MyDocument ( , ) , xib. , shapeAdded:(NSRect)shape . , , , ( ) ( ) ( ), .

+3
3

cromulent . NSArray arrangedObjects, content , , . , .

, IBPlugin, IB, , bind:toObject:withKeyPath:options:.

+4

/Developer/Examples/AppKit/Sketch xcode, , , . , "" -. IB, .

+2

There are several similarities between your code and NSTableView, so I would probably look at a data source (similar to your delegate) or even a binding.

+1
source

Source: https://habr.com/ru/post/1705328/


All Articles