创建许多具有不同内容的视图控制器并在它们之间移动



我在呈现图像的故事板中有一个ViewController,我希望有"next"和"prev"按钮来移动包含不同图像的同一类的实例(10个viewControllers)。"第一个"实例是故事板的一部分,我手动编程了滚动条子视图的所有图像。现在的问题是如何正确添加手动视图控制器

  1. 我该如何处理它?我一直在想,由于只有图像会改变,我应该制作一个像"initWithImageArray"这样的实例方法吗?
  2. 我应该创建一个类实例的可变数组吗?
  3. 如果是这样,有没有办法知道我是数组的哪个实例? 所以我会知道何时"停止"而不显示"下一步"按钮或"上一个"按钮

    VCDecorations *page1 = [self.storyboard instantiateViewControllerWithIdentifier:@"VCDecorations"];
    VCDecorations *page2 = [self.storyboard instantiateViewControllerWithIdentifier:@"VCDecorations"];
    VCDecorations *page3 = [self.storyboard instantiateViewControllerWithIdentifier:@"VCDecorations"];
    DecoViewControllers = [[NSArray alloc]initWithObjects:page1,page2,page3, nil];
    

有没有知道我目前是在"页面 1"还是"页面 2"?

谢谢。

不使用不同的视图控制器使用单个视图控制器并将所有图像添加到数组中,当点击下一个或上一个按钮时,根据您的数组更改图像视图中的图像。

试试这个例子:

self.imageArray = [NSArray arrayWithObjects:@"image1

.png",@"image2.png"],@"image3.png"],@"image4.png"],@"image5.png"],@"image6.png"],nil];
self.currentIndex = 0;
-(IBAction)nextImageClicked:(id)sender{
    if (self.currentIndex < self.imageArray.count-1) {
    self.currentIndex++;
    NSString *imageName = [NSString stringWithFormat:@"%@",[self.imageArray objectAtIndex:self.currentIndex]];
    UIImage * image  = [UIImage imageNamed:imageName];
    self.imageView.image = image;
   }
}
 -(IBAction)prevImageClicked:(id)sender{
if (self.currentIndex > 0) {
    self.currentIndex--;
    NSString *imageName = [NSString stringWithFormat:@"%@",[self.imageArray objectAtIndex:self.currentIndex]];
    UIImage * image  = [UIImage imageNamed:imageName];
    self.imageView.image = image;
  }
}

最新更新