Yevgeni
Yevgeni

Reputation: 1533

Creating many ViewControllers with different content and moving between them

Im having a ViewController in a storyboard that presents images, i want to have "next" and "prev" buttons to move through instances(10 viewControllers) of the same class that contains different images. The "first" instance is part of a storyboard, i programmed manually all the images that are subview of a scrollbar. Now the question is how to add properly manual ViewControllers

  1. How do i approach it? Ive been thinking and since only the images change should i make a instance method like "initWithImageArray" ?
  2. Should i create a MutableArray of the class instances ?
  3. If so , is there a way to know which instance of the array i am ? so i would know when to "stop" and not display "next" button or "prev" button

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

Is there away to know if i"m currently in "page1" or "page2" ?

Thank you.

Upvotes: 0

Views: 65

Answers (2)

Dipak Makwana
Dipak Makwana

Reputation: 29

Try this Example:

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

Upvotes: 0

SreeHarsha
SreeHarsha

Reputation: 506

Instead of using different viewControllers use single viewController and add all images in a an array and when next or Previous button is tapped change the image in imageView according to your array.

Upvotes: 2

Related Questions