首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >键值编码投诉错误

键值编码投诉错误
EN

Stack Overflow用户
提问于 2014-05-14 10:37:28
回答 1查看 277关注 0票数 1

我正在使用这段代码水平滚动页面,但是我得到了这个错误:终止应用程序,原因是:“<__NSCFString 0xc76a3b0> valueForUndefinedKey::这个类不符合键图像的键值编码。”

为什么会出现这个错误,以及如何纠正这个错误。

代码语言:javascript
复制
-(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;



}

我正在使用:

代码语言:javascript
复制
  NSURL *imgurl =[NSURL URLWithString:[[Dic valueForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    self._imageView1.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:imgurl]];

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-14 10:58:44

应该有两个原因。

  1. Dic可能不是NSDictionary 如果( [Dic isKindOfClass:isKindOfClass]){ NSURL *imgurl =[NSURL URLWithString:[Dic / valueForKey:@"image“self._imageView1.image=][UIImage imageWithData:NSData dataWithContentsOfURL:imgurl]];
  2. _imageView1不是UIImageView
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23652251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档