How to make multiple viewcontrollers from different instances of a class/enum in Swift

Question

I am a beginner so apologies if this seems like a trivial question.

I'm trying to make an app where there is an image and a label on each viewcontroller.

There are a total of 10 viewcontrollers and I would prefer to not have to make all ten in main.storyboard (i.e. hard code each viewcontroller) and would rather code so that if I swipe to the left (or press a next button) it loads up by coding for this.

I made 2 xcode projects. 1 where I used an enum with a case for each viewcontroller and used extensions to add a string var and a var UIImage to each case. And the second I used a class that has a text and image property, made ten instances and for each viewcontroller and put them in an array.

Question 1: Which method is better? Question 2: What would I have to do allow the viewcontrollers to show up by swiping left or pressing a next button (I don't mind either method) without hardcoding each viewcontroller?

Thanks in advance! Below is the code I did for method 1:

import UIKit


class Page: UIViewController {

@IBOutlet weak var image: UIImageView!
@IBOutlet weak var text: UITextField!



var story: Story?

override func viewDidLoad() {
    super.viewDidLoad()

    image.image = Story.page1.image
        text.text = Story.page1.text


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

enum Story {

case page1
case page2
case page3
case page4
case page5
case page6
case page7
case page8
case page9
case page10

}

extension Story {
var image: UIImage {
    switch self {

    case .page1: return #imageLiteral(resourceName: "one")
    case .page2: return #imageLiteral(resourceName: "two")
    case .page3: return #imageLiteral(resourceName: "three")
    case .page4: return #imageLiteral(resourceName: "four")
    case .page5: return #imageLiteral(resourceName: "five")
    case .page6: return #imageLiteral(resourceName: "six")
    case .page7: return #imageLiteral(resourceName: "seven")
    case .page8: return #imageLiteral(resourceName: "eight")
    case .page9: return #imageLiteral(resourceName: "nine")
    case .page10: return #imageLiteral(resourceName: "ten")

    }


  }
  var text: String {
    switch self {
    case .page1: return "This is page 1"
    case .page2: return "This is page 2"
    case .page3: return "This is page 3"
    case .page4: return "This is page 4"
    case .page5: return "This is page 5"
    case .page6: return "This is page 6"
    case .page7: return "This is page 7"
    case .page8: return "This is page 8"
    case .page9: return "This is page 9"
    case .page10: return "This is page 10"
    }


  }



  }

Show source
| class   | swift   | enums   2017-08-16 15:08 0 Answers

Answers to How to make multiple viewcontrollers from different instances of a class/enum in Swift ( 0 )

Leave a reply to - How to make multiple viewcontrollers from different instances of a class/enum in Swift

◀ Go back