swifttableviewside-menuvip

How do I change the UIimageview image in the ViewController when the side menu is clicked?


I want to change the background image in the Viewcontroller when one of the side menu items is clicked.

I am using VIP architecture while doing this process.

When the item on the side menu is clicked, the following code block works:

  else if indexPath.row == 1

    {
        switch indexPath.section {
        case 0:
            print("Any ")
            print(indexPath)
        case 1:
            print("Dark Mode ")
          //  self.interactor?.selectThemes(theme: "Dark")
            
            router?.changeTheme(theme: .Dark)
            

        case 2:
            print("Once a day ")
            
        default:
            print("out of range")
        }
    }

then there is something I want to do on the router:

func changeTheme(theme:Themes){
    if Themes.Dark == theme
    {
       var jokegenerate = JokeGenerateController()
        
        jokegenerate.changeBackground(image: UIImage(named: "duck")!)
       
        //print(theme)
        
    }

}

Finally, I want to change the image in the viewController. my code is below:

  func changeBackground(image: UIImage){
    backGroundImage.image = image
    print("hey")
}

But I am getting the following error here: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

What should I do to fix the error?


Solution

  • You should check this link (delegate)

    You should create connections between modules.

    class Bakery
    {
        var delegate:BakeryDelegate?
    
        func makeCookie()
        {
            var cookie = Cookie()
            cookie.size = 6
            cookie.hasChocolateChips = true
    
            delegate?.cookieWasBaked(cookie)
        }
    }
    
    class CookieShop: BakeryDelegate
    {
        func cookieWasBaked(_ cookie: Cookie)
        {
            print("Yay! A new cookie was baked, with size \(cookie.size)")
        }
    }
    let shop = CookieShop()
    
    let bakery = Bakery()
    bakery.delegate = shop
    
    bakery.makeCookie()
    

    Something like that.