我有一个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
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
答案 0 :(得分:0)
在你问题的开头提到它的类的名称ImageBrowserController
有一些混淆,看起来在你的问题结束时它被称为BrowserController
子>
问题是您在_images
中分配awakeFromNib
,因为该类是通过单例模式(sharedInstance
创建的)而不是从.nib
加载的,所以永远不会调用awakeFromNib
文件。
因此,请将代码从init
移至awakeFromNib
并转储- (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;
}
:
BrowserController.m:
initWithFrame
进一步混淆:您已实施{{1}}方法。为什么呢?