简体   繁体   中英

How to communicate between two NSView objects?

With Cocoa, I hope to display one image in one image view. Whenever I click on the showed image. A piece of predefined-size of the image around the click point will be shown in another image view in the same window with the first one. So details will be seen.

To do this, I write my own MyImageView inherited from NSImageView. In this class, I implement the mouseUp method. In this method, I do all the image coordinate things. I can do this right so far. And the new piece will be stored as a property.

#import "MyImageView.h"

@implementation MyImageView
@synthesize point;
@synthesize small_img;
@synthesize big_img;

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.point = NSMakePoint(0.0, 0.0);
    }
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
        [super drawRect:dirtyRect];

    // Drawing code here.
}

- (void)mouseUp:(NSEvent *)theEvent
{
    //calculate the small image and stored in small_img
}

@end

Then I initialize an instance in the AppDelegate and do something like [small_view setImage: myImageView.small_img] But no small image will show.

It seems a delegate mechanism will work out. However, I am very confused with the View-Controller-Delegate pattern. And I can't find any material explaining the communication between two NSView subclass objects with sample code I can understand. I am totally a novice in this domain.

Hope somebody help me get this done since it's very important to me.Thanks!

You're right about the delegate mechanism could work. But there are other options.

Basically you have two ImageViews (or subclasses) and they can't communicate. The only piece they have in common is the container or owner (usually a ViewController, but it could be the AppDelegate), so let's call this object just the owner .

To communicate, the clicked ImageView would need to notify the owner that something happened, and the owner in turn would forward this notification to the second ImageView. The first notification can be done through a delegate , and the second method is straightforward (calling [mySecondImageView someMethod]; )

It would be too long to explain here how to use a delegate, and there are many examples out there. In short, the first ImageView has a delegate property and the owner sets itself as the delegate (something like myFirstImageView.delegate = self];

If this is too complicated, another solution which might fit well here is using Notifications. Again, there are many examples, but in short, a notification would allow communicating your two ImageViews without intervention of the owner . This type of communication is good for loosely coupled objects.

EDIT:
You can of course set the second view as the delegate and it would work perfectly. I personally like to centralize control over all my objects in my ViewController, but this is my personal preference.

What kind of delegate you need? You would need to create a custom delegate (a @protocol ).

@protocol imageViewProtocol
- (void)imageClicked:(... arguments...); 
@end

Your FirstImageView would have a property declared in the @interface .

@interface FirstImageView ...
@property (...) id<imageViewProtocol> delegate;
...
@end

@implementation FirstImageView ...
- (void)ImageClick:(id)sender
{
    // do stuff
    [self.delegate imageClicked:(...arguments...)];
}
@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM