开发者

-[ImageScrollView index2]: unrecognized selector sent to instance

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientat  ion duration:(NSTimeInterval)duration {
UIInterfaceOrientation o = self.interfaceOrientation;
if ((o == UIInterfaceOrientationPortrait) || (o == UIInterfaceOrientationPortraitUpsideDown))   {D = 1;[self setPaging];}
if ((o == UIInterfaceOrientationLandscapeLeft) || (o == UIInterfaceOrientationLandscapeRight))  {D = 2;[self setPaging];}}

I am duplicating some of my methods so that I can have one view for portrait and another view for landscape mode. Mostly it has worked for example:

 //-----------------------------------------------------------------------------*
 -(ImageScrollView*)imagescrollview{
if(imagescrollview == nil){
imagescrollview = [[viewClass alloc] initWithFrame:CGRectZero];}
return imagescrollview;}
//--*duplicate
-(ImageScrollView2*)imagescrollview2{
if(imagescrollview2 == nil){
imagescrollview2 = [[viewClass alloc] initWithFrame:CGRectZero];}
return imagescrollview2;}
//-----------------------------------------------------------------------------*
- (ImageScrollView *)dequeueRecycledPage{
ImageScrollView *page = [recycledPages anyObject];
if (page) {
    [[page retain] autorelease];
    [recycledPages removeObject:page];}
return page;}
//--*duplicate
- (ImageScrollView2 *)dequeueRecycledPage2{
ImageScrollView2 *page = [recycledPages anyObject];
if (page) {
    [[page retain] autorelease];
    [recycledPages removeObject:page];}
return page;}

The code above (my duplicated method(s)) works! But the code below causes the app to crash...

//-----------------------------------------------------------------------------*
- (void)tilePages {
if (D == 1) {
// Calculate which pages are visible
CGRect visibleBounds = pagingScrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) /     CGRectGetWidth(visibleBounds));
int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex  = MIN(lastNeededPageIndex, [self pdfPageCount] - 1);
// Recycle no-longer-visible pages 
for (ImageScrollView *page in visiblePages) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];}}
[visiblePages minusSet:recycledPages];
// add missing pages
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
ImageScrollView *page = [self dequeueRecycledPage];
if (page == nil) {
 page = [[[ImageScrollView alloc] init] autorelease];}
        [self configurePage:page forIndex:index];
        [pagingScrollView addSubview:page];
[visiblePages addObject:page];}}}
else if (D == 2) {
// Calculate which pages are visible
CGRect visibleBounds = pagingScrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) /    CGRectGetWidth(visibleBounds));
int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex  = MIN(lastNeededPageIndex, [self pdfPageCount] - 1);
// Recycle no-longer-visible pages 
for (ImageScrollView2 *page in visiblePages) {
if (page.index2 < firstNeededPageIndex || page.index2 > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];}}
[visiblePages minusSet:recycledPages];
// add missing pages
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
ImageScrollView2 *page = [self dequeueRecycledPage2];
if (page == nil) {
 page = [[[ImageScrollView2 alloc] init] autorelease];}
[self configurePage2:page forIndex2:index];
[pagingScrollView addSubview:page];
[visiblePages addObject:page];}}}}


//-----------------------------------------------------------------------------*
- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index{
page.index = index;
page.frame = [self frameForPageAtIndex:index];
[page displayTiledImageNamed:[self pdfPage: index] size:[self pdfSize: index]];}
//--*duplicate
- (void)configurePage2:(ImageScrollView2 *)page forIndex2:(NSUInteger) index {NSLog(@"1");
page.index2 = index;NSLog(@"4");
page.frame = [self frameForPageAtIndex:index];NSLog(@"5");
[page displayTiledImageNamed2:[self pdfPage: index] size:[self pdfSize: index]];NSLog(@"6");}

the crash has some thing to do with index2 (index2 is a duplicate of index) see here:

//-----------------------------------------------------------------------------*
@interface ImageScrollView : UIScrollView <UIScrollViewDelegate> {
UIView        *imageView;
NSUInteger     index;
}
@property (assign) NSUInteger index;
- (void)displayTiledImageNamed:(CGPDFPageRef)page size:(CGSize)imageSize;
@end
//--*duplicate
@interface ImageScrollView2 : UIScrollView <UIScrollViewDelegate> {
UIView        *imageView;
NSUInteger     index2;
}
@property (assign) NSUInteger index2;
- (void)displayTiledImageNamed2:(CGPDFPageRef)page size:(CGSize)imageSize;
@end

The error in the console is :

2010-08-22 03:02:36.532 PDFmag[31408:207] p4
2010-08-22 03:02:36.541 PDFmag[31408:207] *** -[ImageScrollView index2]: unrecognized   selector sent to instance 0x148d10
2010-08-22 03:02:36.551 PDFmag[31408:207] *** Terminating app due to uncaught exception   'NSInvalidArgumentException', reason: '*** -[ImageScrollView index2]: unrecognized selector  sent to instance 0x148d10'
2010-08-22 03:02:36.565 PDFmag[31408:207] Stack: (
864992541,
859229716,
864996349,
864492313,
864454720,
21249,
18449,
21417,
838908048,
839266120,
839260480,
864766485,
839108308,
839107980,
839226672,
839225216,
839206800,
839205012,
875886564,
864740651,
864738335,
875880904,
838872112,
838865456,
11645,
11560
)
terminate called after throwing an instance of 'NSException'
Program rec开发者_JS百科eived signal:  “SIGABRT”.

NSLog(@"p4"); the app looks like it crashes here!! I am not sure what has happened to cause this, please advise me.

Thank you.


First... wot? Why are you creating two subclasses, one w/index and the other with index2? That is tremendously confusing and would, at first glance, seem to be completely unnecessary given standard OO design patterns.

It also looks to be the root cause of your problem. Namely, you are passing an instance of ImageScrollView where you mean to be passing an instance of ImageScrollView.

The first thing I'd do is get rid of all the 2 nonsense on the method names. There is no reason for ImageScrollView2 to have an index2. Heck, you could even:

@interface AbstractImageScrollView : UIScrollView <UIScrollViewDelegate> {
UIView        *imageView;
NSUInteger     index;
}
@property (assign) NSUInteger index;
- (void)displayTiledImageNamed:(CGPDFPageRef)page size:(CGSize)imageSize;
@end

And then:

@interface ImageScrollView1 : AbstractImageScrollView
...
@end

@interface ImageScrollView2 : AbstractImageScrollView
...
@end

Implement the common behavior in the abstract superclass and the unique behaviors in the two subclasses, including overriding -displayTileImageNamed:size:, if necessary.


bbum, I did what you said, I like the idea it is very neat & tidy but...

@implementation ISVportrate : ImageScrollView 
warning: cannot find interface declaration for 'ISVportrate'
error: property 'index' attempting to use ivar 'index' declared in super class of 'ISVportrate'
error: request for member 'showsVerticalScrollIndicator' in something not a structure or union
error: request for member 'showsHorizontalScrollIndicator' in something not a structure or union
error: request for member 'bouncesZoom' in something not a structure or union
error: request for member 'clipsToBounds' in something not a structure or union
error: request for member 'decelerationRate' in something not a structure or union
error: request for member 'delegate' in something not a structure or union
error: request for member 'zoomScale' in something not a structure or union
error: request for member 'contentSize' in something not a structure or union
error: request for member 'maximumZoomScale' in something not a structure or union
error: request for member 'minimumZoomScale' in something not a structure or union
error: request for member 'zoomScale' in something not a structure or union
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜