Unexpectedly Found Nil Iboutlet in Prepareforsegue

Unexpectedly found nil IBOutlet in prepareForSegue

You cannot access IBOutlets of DetailViewController in prepareForSegue because they are not connected yet.

As mentioned in the comments create a String property in DetailViewController, set it in prepareForSegue and set the text property of the label in viewDidLoad or viewWillAppear of DetailViewController.

UILabel in segue.destination - Unexpectedly found nil while implicitly unwrapping an Optional value

Solution 1 call loadViewIfNeeded()

let destinationVC = segue.destination as! AnnouncementViewController
destinationVC.view.loadViewIfNeeded()
destinationVC.set(announcement)

Solution 2

let destinationVC = segue.destination as! AnnouncementViewController 
destinationVC.ann = announcement


var ann = [String: String]()

override func viewDidLoad() {
super.viewDidLoad()
set(ann)
}

func set(_ announcement: [String: String]) {
print(announcement)

nameLabel.text = announcement["name"] // no error
self.infoTV.text = announcement["info"]
self.mail = announcement["email"]!
self.url = announcement["url"]!

}

unexpectedly found nil while unwrapping an Optional value prepareForSegue

This basically means your IBOutlets are not yet initialised.

You should set strings and then in the viewDidLoad set you labels.
So to sum up:

  1. Add string properties in your DetailsViewController
  2. Set these string properties in your preparForsegue function
  3. in the viewDidLoad of your DetailsViewController, set your labels

Your code should look like something like this :

//In my CollectionView Controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

if(segue.identifier == "DetailsView")
{
if let vc:DetailsViewController = segue.destinationViewController as? DetailsViewController
{
vc.detailsString = self.description
vc.lineString = self.subText
vc.startTimeString = self.formatted_time
}
}
}

//DetailsViewController
import UIKit

class DetailsViewController: UIViewController {

@IBOutlet var startTime: UILabel!
@IBOutlet var line: UILabel!
@IBOutlet var details: UILabel!

var startTimeString: String?
var lineString: String?
var detailsString: String?

override func viewDidLoad() {
super.viewDidLoad()


startTime.text = tmpStartTimeString
line.text = tmpLineString
details.text = tmpDetailsString

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()
}

}

Double check my code, I've wrote it very quickly ;)

prepare segue unexpectedly found nil while unwrapping an Optional value

Sahil's answer is on track, but doesn't address that valueInput is also an optional. Use this code:

if let valueInput = valueInput,
let val = valueInput.text {
value = Int(val)
}

In addition regarding properly unwrapping the optional valueInput I wanted to add that chances are that if valueInput is an IBOutlet it's defined as:

@IBOutlet weak var valueInput: UITextField!

That's called an implicitly unwrapped optional. The annoying thing is that since it is also an IBOutlet, if it ever becomes disconnected from the UITextField in Interface Builder, the variable will become nil, and accessing it from anywhere in the code will cause a crash.

Change it to this:

@IBOutlet weak var valueInput: UITextField?

I've also written about this on my blog: http://cleanswifter.com/implicitly-unwrapped-iboutlets/

Note: you didn't show the definition of valueInput so I assumed it to be a UITextField

Error: Found nil while unwrapping an Optional value; While Pass Data to the New Controller

By doing this: destination!.label.text = valueToPass, you are already setting some text to the label before even it is drawn or presented.

Rather use some variable to pass data to next view and show that data inside viewDidLoad.

let destination = segue.destinationViewController as? web___ViewController

destination!.strValue = valueToPass

Web_VC:

var strValue:String?

func viewDidLoad(){
super.viewDidLoad()

label.text = strValue!
}

Unexpectedly found nil while assigning TO an optional

The issue might be in where you call the setTimer function.
Try to run it in viewDidLoad of TimeControlVC and see what happens.

You shouldn't call it (setTimer function) before pushing or presenting your view controller because the view is not set up yet hence it will crash by throwing nil error because they can't find the view that you want to assign to.



Related Topics



Leave a reply



Submit