简体   繁体   中英

key value coding complaint error

i am using this code to scroll the pages horizontally but i am getting this error: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0xc76a3b0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key image.'

why is this error coming and how to rectify this.

-(void)loadScrollView

{

    _scrollView.contentSize = CGSizeMake(0, _scrollView.frame.size.height);

  //  [[SharedUtilities getInstance]RemoveActivityIndicatorView];

    NSMutableArray *controllers1 = [[NSMutableArray alloc] init];

    for (unsigned i = 0; i < [_arrUrlLinks count]; i++) {

        [controllers1 addObject:[NSNull null]];

    }

    self.viewControllers1 = controllers1;
    count=1;


    // a page is the width of the scroll view

    _scrollView.pagingEnabled = YES;

    _scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width * [_arrUrlLinks count], _scrollView.frame.size.height);

    _scrollView.showsHorizontalScrollIndicator = NO;

    _scrollView.showsVerticalScrollIndicator = NO;

    _scrollView.scrollsToTop = NO;

    _scrollView.delegate = self;


    pageControl.numberOfPages = [_arrUrlLinks count];

    pageControl.currentPage = 0;





    // pages are created on demand

    // load the visible page

    // load the page on either side to avoid flashes when the user starts scrolling

    [self loadScrollViewWithPage:0];

    [self loadScrollViewWithPage:1];

}





- (void)loadScrollViewWithPage:(int)page {

    if (page < 0) return;

    if (page >= [_arrUrlLinks count])

        return;


    // replace the placeholder if necessary

    controller1 = [viewControllers1 objectAtIndex:page];

    if ((NSNull *)controller1 == [NSNull null]) {

        NSString *deviceType = [UIDevice currentDevice].model;

        if([deviceType isEqualToString:@"iPhone"])

        {

            controller1 = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

        }

        else{

            controller1 = [[MyViewController alloc] initWithNibName:@"MyViewController_ipad" bundle:nil];

        }

        [controller1 initWithPageNumber:page];

        [controller1 setArrData:_arrUrlLinks];

        [viewControllers1 replaceObjectAtIndex:page withObject:controller1];

        //[controller release];

    }


    // add the controller's view to the scroll view

    if (nil == controller1.view.superview) {

        CGRect frame = _scrollView.frame;

        frame.origin.x = frame.size.width * page;

        frame.origin.y = 0;

        controller1.view.frame = frame;

        [_scrollView addSubview:controller1.view];

    }

}

- (void)unloadScrollViewWithPage:(int)page {

    if (page < 0) return;

    if (page >= [_arrUrlLinks count]) return;



    controller1 = [viewControllers1 objectAtIndex:page];



    if ((NSNull *)controller1 != [NSNull null]) {

        if (nil != controller1.view.superview)

            [controller1.view removeFromSuperview];



        [viewControllers1 replaceObjectAtIndex:page withObject:[NSNull null]];

        //        [[NSURLCache sharedURLCache] removeAllCachedResponses];

    }

}



- (void)scrollViewDidScroll:(UIScrollView *)sender {

    // We don't want a "feedback loop" between the UIPageControl and the scroll delegate in

    // which a scroll event generated from the user hitting the page control triggers updates from

    // the delegate method. We use a boolean to disable the delegate logic when the page control is used.

    if (pageControlUsed) {

        // do nothing - the scroll was initiated from the page control, not the user dragging

        return;

    }


    // Switch the indicator when more than 50% of the previous/next page is visible

    CGFloat pageWidth = _scrollView.frame.size.width;

    int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    pageControl.currentPage = page;

    //    NSLog(@"current page %d",page);


    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)

    [self unloadScrollViewWithPage:page - 2];

    [self loadScrollViewWithPage:page - 1];

    [self loadScrollViewWithPage:page];

    [self loadScrollViewWithPage:page + 1];

    [self unloadScrollViewWithPage:page + 2];

    count=page+1;


  //  [self newCountTitleSet];

    // A possible optimization would be to unload the views+controllers which are no longer visible

}



// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollViewLoc

{

    CGFloat pageWidth = scrollViewLoc.frame.size.width;

    CGPoint translation = [scrollViewLoc.panGestureRecognizer translationInView:scrollViewLoc.superview];

    int page = floor((scrollViewLoc.contentOffset.x - pageWidth / 2) / pageWidth) + 1;



    if(translation.x > 0)

    {

        if(count!=0)

        {

            if(page==0)

            {

                [self DownLoadDataPrevious:count-1];

            }

        }

        else{

            if(page==1)

            {

                [btnPreviousNews setImage:[UIImage imageNamed:@"arrow2_prev.png"] forState:UIControlStateNormal];

                btnPreviousNews.userInteractionEnabled=FALSE;

            }

        }



    } else

    {

        btnPreviousNews.userInteractionEnabled=TRUE;

        [btnPreviousNews setImage:[UIImage imageNamed:@"arrow1_prev.png"] forState:UIControlStateNormal];

        if(count+1!=[_arrUrlLinks count])

        {

            if(page+1==[_arrUrlLinks count])

            {

                [self DownLoadDataNext:count+1];


            }

            count=count+1;

        }



    }

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    pageControlUsed = NO;

}


- (IBAction)changePageToNext:(id)sender {

    btnPreviousNews.userInteractionEnabled=TRUE;

    [btnPreviousNews setImage:[UIImage imageNamed:@"arrow1_prev.png"] forState:UIControlStateNormal];

    int page = count;

    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)

    [self loadScrollViewWithPage:page - 1];

    [self loadScrollViewWithPage:page];

    [self loadScrollViewWithPage:page + 1];



    // update the scroll view to the appropriate page

    CGRect frame = scrollView.frame;

    frame.origin.x = frame.size.width * page;

    frame.origin.y = 0;

    [_scrollView scrollRectToVisible:frame animated:YES];



    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.

    pageControlUsed = YES;


    count=count+1;

   // [self newCountTitleSet];


}



- (IBAction)changePageToPrev:(id)sender {

    //    CGFloat pageWidth = scrollView.frame.size.width;

    //    int page2 = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;

    //    NSLog(@"current page %d",page2);

    //

    count=count-1;

    NSLog(@"count %d",count);

    int page = count-1;

    NSLog(@"page %d",page);



    if(count==0)

    {

        if(page==0)

        {

            [btnPreviousNews setImage:[UIImage imageNamed:@"arrow2_prev.png"] forState:UIControlStateNormal];

            btnPreviousNews.userInteractionEnabled=FALSE;

        }

        if(page>=0)

        {

           // [self newCountTitleSet];

        }

    }

    else{

        if(page<0)

        {

            [self DownLoadDataPrevious:count-1];

            _lblNewsCount.text=[NSString stringWithFormat:@"%d/%d",1,[_arrUrlLinks count]];

        }

        else{

           // [self newCountTitleSet];

        }

    }


    // load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)

    [self loadScrollViewWithPage:page - 1];

    [self loadScrollViewWithPage:page];

    [self loadScrollViewWithPage:page + 1];



    // update the scroll view to the appropriate page

    CGRect frame = _scrollView.frame;

    frame.origin.x = frame.size.width * page;

    frame.origin.y = 0;

    [_scrollView scrollRectToVisible:frame animated:YES];



    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.

    pageControlUsed = YES;



}

i am using :

  NSURL *imgurl =[NSURL URLWithString:[[Dic valueForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    self._imageView1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:imgurl]];

to get the images from the dictionary and show them on scroll view

There should be a two reasons.

  1. Dic might not be the NSDictionary

     if( [Dic isKindOfClass:[NSDictionary class]] ) { NSURL *imgurl =[NSURL URLWithString:[[Dic valueForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; self._imageView1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:imgurl]]; } 
  2. _imageView1 is not UIImageView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM