1

I have a IKImageBrowserView which has its own controller - BrowserController.h + .m

@interface BrowserController : NSWindowController{
    NSMutableArray *_images;
}
@property (strong,nonatomic) IBOutlet IKImageBrowserView *imageBrowser;

-(void)addMultipleImages;

When I run the app for the first time, the staring image loads, but when I click a button to add other images and call a method from another class I don't get any results. I have noticed that my _imageBrowser loses the reference and becomes nil.

How could I solve this issue?

AppDelegate.m

#import "AppDelegate.h"
#import "BrowserController.h"

@implementation AppDelegate{
    BrowserController *imageBrowserController;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    imageBrowserController = [BrowserController sharedManager];

}

- (IBAction)doSomething:(id)sender {
    [imageBrowserController addMultipleImages];
}


@end

BrowserController.m

#import "BrowserController.h"
@interface myImageObject : NSObject
{
    NSString *_path;
}
@end


@implementation myImageObject

/* our datasource object is just a filepath representation */
- (void)setPath:(NSString *)path
{
    if(_path != path)
    {
        _path = path;
    }
}

/* required methods of the IKImageBrowserItem protocol */
#pragma mark -
#pragma mark item data source protocol

/* let the image browser knows we use a path representation */
- (NSString *)imageRepresentationType
{
    return IKImageBrowserPathRepresentationType;
}

/* give our representation to the image browser */
- (id)imageRepresentation
{
    return _path;
}

/* use the absolute filepath as identifier */
- (NSString *)imageUID
{
    return _path;
}

@end


@interface BrowserController ()

@end

@implementation BrowserController

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

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

    // Drawing code here.
}

- (void)awakeFromNib{

    myImageObject *p;
    p = [[myImageObject alloc]init];
    [p setPath:[[NSBundle mainBundle]pathForResource:@"Unknown" ofType:@"jpg"]];
    _images = [[NSMutableArray alloc]init];
    [_images addObject:p];

    [self updateDataSource];
}


- (void)updateDataSource{

    [_imageBrowser reloadData];
}

-(NSUInteger) numberOfItemsInImageBrowser:(IKImageBrowserView *)aBrowser{
    return [_images count];
};

-(id)imageBrowser:(IKImageBrowserView *)aBrowser itemAtIndex:(NSUInteger)index{
    return [_images objectAtIndex:index];
};

- (void)updateDatasource
{
    [_imageBrowser reloadData];
}

- (void)addMultipleImages{
    myImageObject *p;
    p = [[myImageObject alloc]init];
    [p setPath:[[NSBundle mainBundle]pathForResource:@"Unknown" ofType:@"jpg"]];
    _images = [[NSMutableArray alloc]init];
    [_images addObject:p];
    [_images addObject:p];
    [_images addObject:p];

    [_imageBrowser reloadData];
}


+ (id)sharedManager {
    static BrowserController *sharedMyManager = nil;
    @synchronized(self) {
        if (sharedMyManager == nil)
            sharedMyManager = [[self alloc] init];
    }
    return sharedMyManager;
}


@end
John Topley
  • 113,588
  • 46
  • 195
  • 237
spacecash21
  • 1,331
  • 1
  • 19
  • 41

1 Answers1

0

There is some confusion as to the name of the class where you mention it's called ImageBrowserController at the start of your question and it looks like it's called BrowserController at the end of your question.

The issue is that you allocate _images in awakeFromNib which is never called given the class is created via the singleton pattern (sharedInstance) and not loaded from a .nib file.

Therefore move the code from awakeFromNib into init and dump awakeFromNib:

BrowserController.m:

- (id)init
{
    self = [super init];
    if (self) {
        myImageObject *p = [[myImageObject alloc]init];
        [p setPath:[[NSBundle mainBundle]pathForResource:@"Unknown" ofType:@"jpg"]];
        _images = [[NSMutableArray alloc]init];
        [_images addObject:p];

        [self updateDataSource];
   }
    return self;
}

Further confusion: you have implemented an initWithFrame method. Why?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • Sorry about the confusion with BrowserController and ImageBrowserController. They do the same thing, just been playing around trying to find the problem. So now I have moved everything from AwakeFromNib to init, but now not even the test image gets loaded. Well anyways, here's the full code here: http://we.tl/6p74AqwaIr – spacecash21 Jun 17 '14 at 14:10