简体   繁体   English

uiscrollview不切换图像子视图

[英]uiscrollview not switching image subviews

I'm building a comic viewer app, that consists of two view controllers, the root viewcontroller basically displays a view where a user decides what comic they want to read by pressing a button. 我正在构建一个漫画查看器应用程序,该应用程序由两个视图控制器组成,根视图控制器基本上显示了一个视图,用户可以在其中通过按下按钮来决定要阅读的漫画。 The second viewController actually displays the comic as a uiscrollview with a toolbar and a title at the top. 第二个viewController实际上将漫画显示为uiscrollview,顶部带有工具栏和标题。

So the problem I am having is that the comic image panels themselves are not changing from whatever the first comic you go to if you select another comic after viewing the first one. 因此,我遇到的问题是,如果在查看第一个漫画后选择了另一个漫画,漫画图像面板本身就不会改变。 The way I set it up, and I admit it's not exactly mvc, so please don't hate, anyway the way I set it up is each comic uiscrollview consists of x number of jpg images where each comic set's image names have a common prefix and then a number like 'funny1.jpg', 'funny2.jpg', 'funny3.jpg' and 'soda1.jpg', 'soda2.jpg', 'soda3.jpg', etc... 我设置它的方式,但我承认它并不完全是mvc,所以请不要讨厌,无论如何,我设置它的方式是每个漫画uiscrollview包含x个jpg图像,其中每个漫画集的图像名称都有一个通用前缀然后是数字,例如“ funny1.jpg”,“ funny2.jpg”,“ funny3.jpg”和“ soda1.jpg”,“ soda2.jpg”,“ soda3.jpg”,等等。

so when a user selects a comic to view in the root controller it makes a call to the delegate and sets ivars on instances of the comicviewcontroller that belongs to the delegate (mainDelegate.comicViewController.property) I set the number of panels in that comic, the comic name for the title label, and the image prefix. 因此,当用户选择漫画在根控制器中查看时,它会调用委托并在属于委托的comicviewcontroller实例(mainDelegate.comicViewController.property)上设置ivars,我设置了该漫画中的面板数,标题标签的漫画名称和图像前缀。

The number of images changes(or at least the number that you can scroll through), and the title changes but for some reason the images are the same ones as whatever comic you clicked on initially. 图片数量发生变化(或至少可以滚动浏览的数量),标题也会发生变化,但由于某些原因,这些图片与您最初单击的漫画相同。

I'm basing this whole app off of the 'scrolling' code sample from apple. 我将整个应用程序都基于苹果的“滚动”代码示例。

I thought if I added a viewWillAppear:(BOOL) animated call to the comicViewController everytime the user clicked the button that would fix it but it didn't, after all that is where the scrollview is laid out. 我以为,如果用户每次单击可修复该问题的按钮时都会向comicViewController添加一个viewWillAppear:(BOOL)动画调用,但实际上并没有,毕竟这是滚动视图的布局。

Anyway here is some code from each of the two controllers: 无论如何,这是来自两个控制器的一些代码:

RootController: RootController:

-(IBAction) launchComic2{

  AppDelegate *mainDelegate = [(AppDelegate *) [UIApplication sharedApplication] delegate];
  mainDelegate.myViewController.comicPageCount = 3;
   mainDelegate.myViewController.comicTitle.text = @"\"Death by ETOH\"";
   mainDelegate.myViewController.comicImagePrefix = @"etoh";

   [mainDelegate.myViewController viewWillAppear:YES];
   [mainDelegate.window addSubview: mainDelegate.myViewController.view];

comicViewController: comicViewController:

   -(void) viewWillAppear:(BOOL)animated {


   self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

   // 1. setup the scrollview for multiple images and add it to the view controller
   //
   // note: the following can be done in Interface Builder, but we show this in code for       clarity
   [scrollView1 setBackgroundColor:[UIColor whiteColor]];
   [scrollView1 setCanCancelContentTouches:NO];
   scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
   scrollView1.clipsToBounds = YES;  // default is NO, we want to restrict drawing       within our scrollview
   scrollView1.scrollEnabled = YES;

   // pagingEnabled property default is NO, if set the scroller will stop or snap at       each photo
   // if you want free-flowing scroll, don't set this property.
   scrollView1.pagingEnabled = YES;

   // load all the images from our bundle and add them to the scroll view
   NSUInteger i;
   for (i = 1; i <= self.comicPageCount; i++)
   {

    NSString *imageName = [NSString stringWithFormat:@"%@%d.jpg", self.comicImagePrefix, i];
    NSLog(@"%@%d.jpg", self.comicImagePrefix, i);
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    CGRect rect = imageView.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    imageView.frame = rect;
    imageView.tag = i; // tag our images for later use when we place them in serial fashion
    [scrollView1 addSubview:imageView];
    [imageView release];
   }

   [self layoutScrollImages]; // now place the photos in serial layout within the scrollview




       }


  - (void)layoutScrollImages
       {
   UIImageView *view = nil;
   NSArray *subviews = [scrollView1 subviews];

   // reposition all image subviews in a horizontal serial fashion
   CGFloat curXLoc = 0;
   for (view in subviews)
   {
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
    {
     CGRect frame = view.frame;
     frame.origin = CGPointMake(curXLoc, 0);
     view.frame = frame;

     curXLoc += (kScrollObjWidth);
    }
   }

   // set the content size so it can be scrollable
   [scrollView1 setContentSize:CGSizeMake((self.comicPageCount * kScrollObjWidth),           [scrollView1 bounds].size.height)];
       }

Any help would be appreciated on this. 任何帮助将不胜感激。

Nick 缺口

So what I ended up doing is just making multiple instances of the comic controller inside the delegate instead of just re-using the one. 因此,我最终要做的就是在委托中创建喜剧控制器的多个实例,而不是仅仅重复使用一个。 Will update this if I run into problems. 如果遇到问题,将对此进行更新。

Nick 缺口

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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