1

我有一个 IKImageBrowserView 它有自己的控制器 - BrowserController.h + .m

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

-(void)addMultipleImages;

当我第一次运行该应用程序时,会加载凝视图像,但是当我单击一个按钮以添加其他图像并调用另一个类的方法时,我没有得到任何结果。我注意到我的 _imageBrowser 丢失了引用并变为 nil。

我该如何解决这个问题?

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

浏览器控制器.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
4

1 回答 1

0

对于您提到它在问题开始时调用的类的名称存在一些混淆,ImageBrowserController并且看起来它是BrowserController在问题结束时调用的。

问题是您分配_imagesawakeFromNib哪个永远不会被调用,因为该类是通过单例模式 ( sharedInstance) 创建的,而不是从.nib文件中加载的。

因此将代码awakeFromNib移入init并转储awakeFromNib

浏览器控制器.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;
}

进一步的困惑:你已经实现了一个initWithFrame方法。为什么?

于 2014-06-17T12:43:13.203 回答