0

我收到此错误,无法解决...

我通过 UISegmentedControl 按钮更改将一个对象从 UIViewController 传递给另一个对象。下面是我用来传递对象的代码。它工作得很好,我可以在控制台中打印对象详细信息。

 @IBAction func switchViewAction(_ sender: UISegmentedControl) {
        let vc = DirectionsViewController()
        if let unwrappedRecipe = self.details
        {
            vc.customInit(recipes: unwrappedRecipe)
        } else

        {
            print("it has no value!")
        }
        self.viewContainer.bringSubviewToFront(views[sender.selectedSegmentIndex])

    }

但是,问题是当我尝试为标签设置值时,出现以下错误:

Unexpectedly found nil while implicitly unwrapping an Optional value

下面是我在 DirectionsViewController 中使用的代码

 @IBOutlet weak var lblDirections: UILabel!
var recipe: Recipe? = nil

override func viewDidLoad()
{
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

func customInit(recipes: Recipe)
{
    lblDirections.text =  recipes.name
}

我研究了可选和强制变量,也尝试安全地解开变量但没有运气。有人可以帮忙吗?

4

2 回答 2

0

如果您有一个IBOutlet从情节提要或 xib 加载的内容,则nil直到viewDidLoad被调用。因此,当func customInit(recipes: Recipe)被调用 before时viewDidLoadlblDirectionsis nil,并且因为它是强制展开的,所以它会崩溃。您可以通过两种方式解决此问题:

  1. 丑陋但容易。当viewDidLoad您第一次获得视图控制器时会调用,view因此您可以_ = vc.viewvc.customInit(recipes: unwrappedRecipe). func switchViewAction(_ sender: UISegmentedControl)我不建议使用它的生产代码,但您可以使用它来测试一切是否正常。

  2. 因为您使用的是 xib,所以您可以初始化视图控制器并提供自定义 init(甚至您的方法名称也表明它:)customInit。要获得正确的视图控制器初始化,您需要使用:


class DirectionsViewController: UIViewController {
   let recipe: Recipe
   @IBOutlet weak var lblDirections: UILabel!

   init(recipe: Recipe) {
       self.recipe = recipe
       let classType = type(of: self)
       let bundle = Bundle(for:classType)
       super.init(nibName: "DirectionsViewController", bundle: bundle) //provide your nib filename 
   }

   override func viewDidLoad(){
       super.viewDidLoad()
       //code from func customInit(recipes: Recipe)
       lblDirections.text =  recipe.name
   }

   @available(*, unavailable, message: "use init(recipe: Recipe)")
   override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
       fatalError("init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) has not been implemented")
   }

   @available(*, unavailable, message: "don't use sotryboard! use nib and init(recipe: Recipe)")
   required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
   }
}

您不能Recipe在创建后提供可选或更新它,但它使代码不那么复杂。它还假定调用了您的 xib 文件DirectionsViewController.xib(如果没有,您需要在 line 中更改它super.init(nibName: "DirectionsViewController", bundle: bundle))。


你也可以使用我的微库NibBased来少写一点代码。使用该库时,您的代码应为:

import NibBased

class DirectionsViewController: NibBaseViewController {
    let recipe: Recipe
    @IBOutlet weak var lblDirections: UILabel!

    init(recipe: Recipe) {
        self.recipe = recipe
        super.init()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        lblDirections.text =  recipe.name
    }
}
于 2019-12-20T10:37:50.633 回答
0

将值传递给 DirectionsViewController 和 DirectionsViewController 调用的 viewDidLoad 方法中的配方变量

    lblDirections.text =  recipe.name
于 2019-12-20T10:39:00.597 回答