- (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];}}

我正在复制某些方法,以便可以有一个人像 View 和一个风景模式 View 。例如,大多数情况下它已经起作用:
 //-----------------------------------------------------------------------------*
 -(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;}

上面的代码(我的重复方法)有效!但是下面的代码导致应用程序崩溃...
//-----------------------------------------------------------------------------*
- (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");}

崩溃与index2有关(index2是index的重复项),请参见此处:
//-----------------------------------------------------------------------------*
@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

控制台中的错误是:
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 received signal:  “SIGABRT”.

NSLog(@“p4”);该应用程序看起来像在这里崩溃了!!我不确定是什么原因导致的,请告知我。

谢谢。

最佳答案

首先...是吗?为什么要创建两个子类,一个子类带有index,另一个子类带有index2?乍看之下,这非常混乱,考虑到标准的OO设计模式,这似乎是完全不必要的。

它也似乎是问题的根本原因。即,您要传递ImageScrollView的实例,这意味着您要传递ImageScrollView的实例。

我要做的第一件事是摆脱方法名称上所有的2废话。 ImageScrollView2没有理由具有index2。哎呀,你甚至可以:

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

接着:
@interface ImageScrollView1 : AbstractImageScrollView
...
@end

@interface ImageScrollView2 : AbstractImageScrollView
...
@end

在抽象 super class 中实现常见行为,并在两个子类中实现唯一行为,包括在必要时覆盖-displayTileImageNamed:size:

关于objective-c - -[ImageScrollView index2]:无法识别的选择器已发送到实例,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3543404/

10-09 19:43