键值编码投诉错误

时间:2014-05-14 10:37:28

标签: ios objective-c

我正在使用此代码水平滚动页面但我收到此错误: 由于未捕获的异常终止应用程序' NSUnknownKeyException',原因:' [< __ NSCFString 0xc76a3b0> valueForUndefinedKey:]:此类不是键映像的键值编码。'

为什么会出现此错误以及如何纠正此问题。

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



}

我正在使用:

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

从字典中获取图像并在滚动视图中显示它们

1 个答案:

答案 0 :(得分:0)

应该有两个原因。

  1. Dic可能不是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不是UIImageView