- (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 received 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.