amber
amber

Reputation: 1

viewDidLoad is not called though it is the root view controller

Hi I am fairly new to swift, the problem is that the viewDidLoad is never called. what am I missing?
the following are the entry point swift code and the ViewContoller code, they are under the same project.

// PolaroidEffectAppApp.swift

import UIKit

@main

class PolaroidEffectAppApp: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // Create a window that fills the screen
        window = UIWindow(frame: UIScreen.main.bounds)
        
        // Check if the window is properly initialized
               guard let window = window else {
                   print("Error: Window could not be initialized.")
                   return false
               }
    
        // Create the root view controller
      **  let rootViewController = ViewController()**
    
        // Create a navigation controller with the root view controller
        let navigationController = UINavigationController(rootViewController: rootViewController)
    
        // Set the window's root view controller to the navigation controller
        window.rootViewController = navigationController
        print("the app will have active view controller.") //printed out successfully in testing
    
        // Make the window visible
        window.makeKeyAndVisible()
    
        return true
    }

}

// ViewController.swift

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    let photoEditorView = PhotoEditorView() // Initialize the PhotoEditorView
    
    override func viewDidLoad() {
        super.viewDidLoad()
        print("View loaded")//did not print in testing
        setupUI()
    }
    
    func setupUI() {
        view.backgroundColor = .white
        
        // Add the PhotoEditorView to the view hierarchy
        view.addSubview(photoEditorView)
        
        // Set constraints or frame for PhotoEditorView
        photoEditorView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            photoEditorView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            photoEditorView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            photoEditorView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            photoEditorView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    
        // Add navigation button to select photo
        let selectPhotoButton = UIBarButtonItem(title: "Select Photo", style: .plain, target: self, action: #selector(selectPhoto))
        navigationItem.rightBarButtonItem = selectPhotoButton
        
        // Connect actions
        photoEditorView.applyButton.addTarget(self, action: #selector(applyEffect), for: .touchUpInside)
    }
    
    @objc func selectPhoto() {
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .photoLibrary
        imagePicker.delegate = self
        present(imagePicker, animated: true, completion: nil)
    }
    
    @objc func applyEffect() {
        guard let image = photoEditorView.imageView.image else { return }
        let grainIntensity = photoEditorView.grainSlider.value
        let scratchIntensity = photoEditorView.scratchSlider.value
        
        let filteredImage = GrainAndScratchFilter.apply(to: image, grainIntensity: grainIntensity, scratchIntensity: scratchIntensity)
        photoEditorView.imageView.image = filteredImage
    }
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let pickedImage = info[.originalImage] as? UIImage {
            photoEditorView.imageView.image = pickedImage
        }
        dismiss(animated: true, completion: nil)
    }

}

I have some break points set up in the @main sections they are all hit, but after it hit "return true" of the func application, it stopped there, it did not hit any of the breakpoints in ViewController

Upvotes: 0

Views: 39

Answers (1)

amber
amber

Reputation: 1

The problem is resolved by creating the project switching from using SwiftUI to storyboard. Then manually remove the storyboard references.

Upvotes: -1

Related Questions