Learn Courses My Dashboard

Scope of IBOutlets

How would I make my scope for IBOutlet var pageControl accessible at file level? Below is the full code and this is the error I am getting:

import UIKit

class WelcomeViewController: UIViewController, UIScrollViewDelegate {
    
    @IBOutlet var holderView: UIView!
    
    @IBOutlet var pageControl: UIPageControl!
    
    let scrollView = UIScrollView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        scrollView.delegate = self
        pageControl.addTarget(self, action: #selector(pageControlDidChange(_:)), for: .valueChanged)
        view.addSubview(pageControl)
        
    }
    
    @objc private func pageControlDidChange(_ sender: UIPageControl) {
        let current = sender.currentPage
        scrollView.setContentOffset(CGPoint(x: CGFloat(current) * view.frame.size.width, y: 0), animated: true)
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        pageControl.frame = CGRect(x: 10, y: view.frame.size.height-100, width: view.frame.size.width-20, height: 70)
        
        
        configure()
        
    }
    
    private func configure() {
        // Set up the scrollview
        
        
        
        scrollView.frame = holderView.bounds
        holderView.addSubview(scrollView)
        
        let titles = ["Welcome!", "Location", "All Set"]
        
        for x in 0..<3 {
            let pageView = UIView(frame: CGRect(x: CGFloat(x) * holderView.frame.size.width, y: 0, width: holderView.frame.size.width, height: holderView.frame.size.height))
            scrollView.addSubview(pageView)
            
            

            // Title, Image & Button
            let label = UILabel(frame: CGRect(x: 10, y: 10, width: pageView.frame.size.width-20, height: 120))
            let imageView = UIImageView(frame: CGRect(x: 10, y: 10+20+10, width: pageView.frame.size.width-20, height: pageView.frame.size.height - 10 - 0 - 10))
            let button = UIButton(frame: CGRect(x: 10, y: pageView.frame.size.height-60, width: pageView.frame.size.width-20, height: 50))
            let button2 = UIButton(frame: CGRect(x: 10, y: pageView.frame.size.height-60, width: pageView.frame.size.width-20, height: 50))
                
                label.textAlignment = .center
                label.font = UIFont(name: "Helvetica-Bold", size: 32)
                pageView.addSubview(label)
                label.text = titles[x]
                                                      
                imageView.contentMode = .scaleAspectFit
                imageView.image = UIImage(named: "welcome_\(x+1)")
                pageView.addSubview(imageView)
                
                button.setTitleColor(.white, for: .normal)
                button.backgroundColor = .systemBlue
                button.setTitle("Continue", for: .normal)
                if x == 2 {
                    
                    
                    let pageView2 = UIView(frame: CGRect(x: CGFloat(x) * holderView.frame.size.width, y: 0, width: holderView.frame.size.width, height: holderView.frame.size.height))
                        scrollView.addSubview(pageView2)
                        button.setTitle("", for: .normal)
                        button2.setTitle("Get Started", for: .normal)
                    
                        pageView2.addSubview(button2)
                    
                    button2.addTarget(self, action: #selector(didTapGetStartedBtn(_:)), for: .touchUpInside)
                    //button2.tag = x+1
                    //pageView2.addSubview(button2)
                    
            }
            
                button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
                button.tag = x+1
                pageView.addSubview(button)
                                        
        }
        scrollView.contentSize = CGSize(width: holderView.frame.size.width*3, height: 0)
        scrollView.isPagingEnabled = true
    }
    
    @objc func didTapButton(_ button: UIButton) {
      
        ViewController.Core.shared.setIsNotNewUser()
        
        guard button.tag < 3 else {

            // dismiss
            dismiss(animated: true, completion: nil)
            return
            
        }
        
        // Scroll to next page
        scrollView.setContentOffset(CGPoint(x: holderView.frame.size.width * CGFloat(button.tag), y: 0), animated: true)

    }
    
    @IBAction func didTapGetStartedBtn(_ button2: UIButton) {
   
       guard let vc = storyboard?.instantiateViewController(withIdentifier: "getStarted") as? GetStartedViewController else {
           print("Failed to print vc from storyboard")
           return
            
        }
        present(vc, animated: true)
        
    }
    

}

extension ViewController: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)))
    }
    
}

You are trying to extend a class called WelcomeViewController, not ViewController.

Make these changes…

class WelcomeViewController: UIViewController, UIScrollViewDelegate

to:

class WelcomeViewController: UIViewController

and

extension ViewController: UIScrollViewDelegate

to:

extension WelcomeViewController: UIScrollViewDelegate

Hey @roosterboy,

That worked great! Thanks!