【问题标题】:unable to set UILabelView.Text value无法设置 UILabelView.Text 值
【发布时间】:2019-12-20 09:23:19
【问题描述】:

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

我通过 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
}

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

【问题讨论】:

  • 这可能是因为您没有将 lblDirections 出口连接到视图控制器中的标签。
  • @P.vanderLaan 已连接

标签: swift xcode forced-unwrapping


【解决方案1】:

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

  1. 丑陋但容易。 viewDidLoad 在你第一次得到视图控制器的view 时被调用,所以你可以在func switchViewAction(_ sender: UISegmentedControl) 中的vc.customInit(recipes: unwrappedRecipe) 之前添加_ = vc.view。我不建议使用它的生产代码,但您可以使用它来测试一切是否正常。

  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(如果不是,则需要在行 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
    }
}

【讨论】:

  • 谢谢。在分析您的答案并摆弄代码之后。我已经设法解决了这个问题。 init 方法是救命稻草。 ☺️ 我在初始化views.append(DirectionsViewController(recipe: self.details!).view) 中传递了配方对象
  • 我认为您还应该保留对整个视图控制器的引用,而不仅仅是它的视图。你可以在这里阅读更多信息:swiftbysundell.com/basics/child-view-controllers
【解决方案2】:

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

    lblDirections.text =  recipe.name

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-09-05
    • 2011-06-08
    • 2014-12-25
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多