【问题标题】:key value coding complaint error键值编码投诉错误
【发布时间】:2014-05-14 10:37:28
【问题描述】:

我正在使用此代码水平滚动页面,但出现此错误: 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[<__nscfstring> 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]];

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

【问题讨论】:

  • 究竟是哪一行导致了问题?
  • 使用故事板,对吧?
  • 不,我使用的是 xib 文件而不是故事板
  • 看起来您正在尝试像字典一样使用 NSString。也许你使用了一个 id 类型的对象并且没有检查它的类。在您的代码中寻找类似valueForKey:(NSString *)objectForKey:(NSString *) 的内容
  • 不,我有一本名为 Dic 的字典,其中有一个名为“images”的键用于存储图像。

标签: ios objective-c


【解决方案1】:

应该有两个原因。

  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

【讨论】:

  • 我只用了“图片”,写错了
  • 字典是 nsdictionary 而_imageView1 是图像视图,所以我不认为这应该是问题。
  • 第一页正在加载,但当我滚动到下一页时,出现此异常
猜你喜欢
  • 1970-01-01
  • 2013-07-12
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 2013-03-30
  • 1970-01-01
相关资源
最近更新 更多