【问题标题】:How do I change the UIimageview image in the ViewController when the side menu is clicked?单击侧边菜单时如何更改 ViewController 中的 UIimageview 图像?
【发布时间】:2021-11-05 02:58:34
【问题描述】:

我想在单击侧边菜单项时更改 Viewcontroller 中的背景图像。

我在做这个过程时使用了 VIP 架构。

当单击侧边菜单上的项目时,以下代码块起作用:

  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")
        }
    }

那么我想在路由器上做一些事情:

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

}

最后,我想更改 viewController 中的图像。我的代码如下:

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

但我在这里收到以下错误: 致命错误:在隐式展开可选值时意外发现 nil

我应该怎么做才能修复错误?

【问题讨论】:

  • 您的资产中是否存在名为“duck”的图像?否则,backGroundImage 是否连接到情节提要中的视图控制器?
  • @Alastar 我在 Asset 中有一张名为“duck”的图片

标签: swift tableview side-menu vip


【解决方案1】:

你应该检查这个链接(delegate)

您应该在模块之间创建连接。

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()

类似的东西。

【讨论】:

    猜你喜欢
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    相关资源
    最近更新 更多