개체

1

I 자체 컨트롤러가있는 IKImageBrowserView를 참조 잃는다 다른 이미지를 추가하고 다른 클래스의 메서드를 호출하려면 결과가 없습니다. 나는 _imageBrowser가 참조를 잃어 버려서 아무 것도 아닌 것으로 나타났습니다.개체

어떻게이 문제를 해결할 수 있습니까?

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 혼란은 당신이 당신의 질문과 그것의 시작 ImageBrowserController을라고 언급 클래스의 이름으로이

#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

@property 자체에 중단 점을 넣고 nil로 설정할 수 있습니다. –

+0

방금 ​​시도했습니다. 앱이 처음로드 될 때 초기화시에만 중단 점을 중지합니다. – spacecash21

+0

실제로 할당하고 시작 하시겠습니까? –

답변

0

귀하의 질문이 끝나면 BrowserController라고 불리는 것 같습니다.

문제는 _images.nib 파일에서로드 싱글 톤 패턴 (sharedInstance) 및하지를 통해 생성 된 클래스를 주어지지라는 절대되는 awakeFromNib에 할당하는 것입니다.

따라서 initawakeFromNib의 코드를 이동하고 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; 
} 

또한 혼란 : 당신이 initWithFrame 방법을 구현했습니다. 왜?

+0

BrowserController 및 ImageBrowserController와 혼동하여 죄송합니다. 그들은 똑같은 일을하고, 문제를 찾기 위해 노력하고 있습니다. 이제 AwakeFromNib에서 init로 모든 것을 옮겼습니다.하지만 이제는 테스트 이미지조차로드되지 않습니다. 어쨌든 여기에 전체 코드가 있습니다. http://we.tl/6p74AqwaIr – spacecash21