0

I have a file in Swift that holds all my queries. And when saving a record with saveOperation.perRecordProgressBlock this file call ChatView view controller and updates the progressBarUpdate function.

So far I can get the print within progressBarUpdate to print the progress just fine. But when I get to update progressBarMessage.setProgress(value!, animated: true) the application just crash with the following error: fatal error: unexpectedly found nil while unwrapping an Optional value

If I try to run progressBarMessage.setProgress(value!, animated: true) through viewDidLoad it updates the progress bar fine, no error. Which means the outlet is working just fine.

Other thing to consider, is that my print(".... perRecordProgressBlock - CHAT VIEW\(value)") works just fine. If gets the updates from Queris.swift. It is just the progressBarUpdate that is causing issues.

@ my Queries.swift file option 1

 saveOperation.perRecordProgressBlock = { (recordID, progress) -> Void in
    print("... perRecordProgressBlock \(Float(progress))")
    var chatView = ChatView()
    chatView.progressBarUpdate(Float(progress))
 }

@ my Queries.swift file option 2

 saveOperation.perRecordProgressBlock = { (recordID, progress) -> Void in
    print("... perRecordProgressBlock \(Float(progress))")

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let chatViewController = storyboard.instantiateViewControllerWithIdentifier("ChatViewVC") as! ChatView
    chatViewController.progressBarUpdate(Float(progress))
}

@ ChatView view controller

func progressBarUpdate(value: Float) 
{
    print(".... perRecordProgressBlock - CHAT VIEW\(value)")
    if (value as? Float) != nil 
    {
        progressBarMessage.setProgress(value, animated: true)
    }
}
GuiSoySauce
  • 1,763
  • 3
  • 24
  • 37

2 Answers2

0
var chatView = ChatView()

I'm going to go out on a limb here and say you are using storyboards/xibs. If so, the above would not be the correct way to instantiate a new view controller. Here's some information on the difference (the question refers to Objective-C but the concept is the same in Swift)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let chatViewController = storyboard.instantiateViewControllerWithIdentifier("identifier-you-set-in-storyboard") as! ChatView

Where identifier-you-set-in-storyboard is set in the interface builder (the linked question is old but illustrates the concept, the field label might have changed in newer versions)


If by some off chance you are creating you are setting up your views in code (as opposed to storyboards), you'd need to call chatView.loadView() before chatView.progressBarUpdate.... (Or just try to access the view property and it should call loadView for you.)

Community
  • 1
  • 1
Andrew
  • 15,357
  • 6
  • 66
  • 101
  • I've made some update to my question with your suggestions. Still not working. Note that the print statement within 'func progressBarUpdate' works just fine. Just the progress bar that is failing. – GuiSoySauce Jun 27 '16 at 04:06
  • @GuiSoySauce Try adding `let _ = chatViewController.view` before calling progressBarUpdate. This will force your view controller to actually load the view and set the progress view property properly. – Andrew Jun 27 '16 at 04:22
0

The way you are instantiating the viewController is not the right way and hence the crash/nil val. viewController loads its view hierarchy only when something sends it a view message. The system will do this by its own when to put the view hierarchy on the screen. And it happens after calls like prepareForSegue:sender: and viewWillAppear: , loadView(), self.view.

So here your outlets are still nil since it is not loaded yet.

Just try to force your viewController to call self.view and then access the functions from that viewController.

Santosh
  • 2,900
  • 1
  • 16
  • 16