Unexpectedly found nil IBOutlet in prepareForSegue
You cannot access IBOutlet
s 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:
- Add string properties in your
DetailsViewController
- Set these string properties in your
preparForsegue
function - in the
viewDidLoad
of yourDetailsViewController
, 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
Swiftui App Life Cycle Ios14 Where to Put Appdelegate Code
Change Uitableview Height Dynamically
How to Parse Json Response from Alamofire API in Swift
#Warning: C-Style For Statement Is Deprecated and Will Be Removed in a Future Version of Swift
How to Handle Different Orientations in Ios
Make Uinavigationbar Transparent
How to Wrap Text in a Uitableviewcell Without a Custom Cell
Resizing Uitableview to Fit Content
Execute Action When Back Bar Button of Uinavigationcontroller Is Pressed
Warning :-Presenting View Controllers on Detached View Controllers Is Discouraged
A Swift Example of Custom Views For Data Input (Custom In-App Keyboard)
Do Something Every X Minutes in Swift
Swift Saving and Retrieving Custom Object from Userdefaults
Swift: Issue in Converting String to Double
String Replacement in Objective-C
iOS (Iphone, iPad, Ipodtouch) View Real-Time Console Log Terminal