I'm developing a feature slider by paginating ScrollView. My application crashes when I call the ViewController of the slider. I tried all possible solutions available on the internet such as clean build, reconnecting the scrollView, etc..., I'm looking for a possible fix. Thanks in advance
Code Block import UIKit class IntroSliderViewController: UIViewController { @IBOutlet weak var slider: UIScrollView! let slide1 = ["background":"sliderBackground-1", "mainImage":"sliderMainPic-1", "heading":"....", "content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aenean suspendisse proin nunc risus sed. Aliquet faucibus sed commodo tellus magna cursus. Eget mauris congue at ornare."] let slide2 = ["background":"sliderBackground-2", "mainImage":"sliderMainPic-2", "heading":"....", "content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aenean suspendisse proin nunc risus sed. Aliquet faucibus sed commodo tellus magna cursus. Eget mauris congue at ornare."] let slide3 = ["background":"sliderBackground-3", "mainImage":"sliderMainPic-3", "heading":".....", "textContent":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi aenean suspendisse proin nunc risus sed. Aliquet faucibus sed commodo tellus magna cursus. Eget mauris congue at ornare."] var slides = [ Dictionary<String,String> ]() override func viewDidLoad() { super.viewDidLoad() slides = [slide1,slide2,slide3] slider.isPagingEnabled = true slider.contentSize = CGSize(width: self.view.bounds.width, height: self.view.bounds.height) slider.showsHorizontalScrollIndicator = false loadSlides() } func loadSlides() { for (index,content) in slides.enumerated(){ if let slide = Bundle.main.loadNibNamed("sliderContent", owner: self, options: nil)?.first as? SliderContentView { slide.backgroundImage.image = UIImage(named: content["background"]!) slide.mainImage.image = UIImage(named: content["mainImage"]!) slide.heading.text = content["heading"] slide.textContent .text = content["textContent"] slider.addSubview(slide) slide.frame.size.width = self.view.bounds.size.width slide.frame.size.height = self.view.bounds.size.height slide.frame.origin.x = CGFloat(index) * self.view.bounds.size.width } } } }
Thanks for showing the code.
This is the worst part of your code:
You are calling the undocumented initializer init() of IntroSliderViewController, in almost all cases, it would create a garbage object which causes Fatal error: Unexpectedly found nil.
When you want to instantiate a view controller that you have defined on the storyboard with subviews and/or IBOutlets/IBActions, you need to instantiate it through the storyboard.
Like this:
(You need to put the right Storyboard ID IntroSliderViewController to the view controller in the Identity Inspector.)
This is the worst part of your code:
Code Block IntroSliderViewController()
You are calling the undocumented initializer init() of IntroSliderViewController, in almost all cases, it would create a garbage object which causes Fatal error: Unexpectedly found nil.
When you want to instantiate a view controller that you have defined on the storyboard with subviews and/or IBOutlets/IBActions, you need to instantiate it through the storyboard.
Like this:
Code Block let introSliderVC = self.storyboard?.instantiateViewController(withIdentifier: "IntroSliderViewController") self.present(introSliderVC, animated: true, completion: nil)
(You need to put the right Storyboard ID IntroSliderViewController to the view controller in the Identity Inspector.)