I have a VC that passes a name to a label on a second view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ViewController{
destination.name = petNameTxt.text
destination.morning = morningFeed.text
destination.night = nightFeed.text
}
}
On the second VC it I try to save the label but it just holds the placeholder text in UserDefaults:
save it in ViewDidLoad
let userDefaults = UserDefaults.standard
userDefaults.setValue(petNameLbl.text, forKey: "data")
userDefaults.synchronize()
load it in a separate function that runs with ViewDidLoad
let defaults = UserDefaults.standard
if let name = defaults.string(forKey: "data"){
petNameLbl.text = name
}
Not sure what your problem is, but here's a more appropriate (INMNSHO) solution:
Code that saves the value:
Thread.current.threadDictionary["data"] = petNameLbl.text
Code that retrieves the value
petNameLbl.text = Thread.current.threadDictionary["data"]
Related
this error happens when I'm passing data from tabelView cell, the data can be read and used in multiple navigationControllers childrens, I have used segues to pass the data, in the second viewController succesfully, the app crash when prfomSegue to the third viewController Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
the tableViewCell contains this data bellow
struct storesData {
var area: String
var title: String
var latitude: Double
var longitued: Double
static func getAllStores()-> [storesData]{
return [
storesData(area: "الخرطوم", title: "KFC", latitude: 15.581010, longitued: 32.569032),
]
}
}
this is how I pass the data
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? Order {
destVC.stores = sender as? storesData
} else if let destVC = segue.destination as? GoogleMap {
destVC.stores = sender as? storesData
}
in the second View controller I'm grabbing the Value of storeData in a variable called stores the navigation title will have the title in the customCell it did work fine
var stores: storesData?
override func viewDidLoad() {
super.viewDidLoad()
title = stores?.title
}
this is where the error came out the last data I'm grabbing is the longitude and the latitude of type Double in the third viewCotroller and its view controller with mapView in it I'm using (GoogleMaps)
var stores: storesData?
override func viewDidLoad() {
super.viewDidLoad()
setUpGoogleMap()
}
func setUpGoogleMap(){
let camera = GMSCameraPosition.camera(withLatitude: 15.592778, longitude: 32.552278, zoom: 12)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
view.addSubview( mapView)
mapView.animate(to: camera)
mapView.isMyLocationEnabled = true
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.delegate = self
let position = CLLocationCoordinate2D(latitude: stores!.latitude, longitude: stores!.longitued)
let marker = GMSMarker(position: position)
marker.map = mapView
}
the compiler shows this error
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/mazenqp/Downloads/DELEVARE - ديليفري/DELEVARE - ديليفري/UserinterFace/GoogleMap.swift, line 239
2020-06-07 18:17:35.717618+0200 DELEVARE - ديليفري[17147:915821] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file /Users/mazenqp/Downloads/DELEVARE - ديليفري/DELEVARE - ديليفري/UserinterFace/GoogleMap.swift, line 239
(lldb)
after searching and searching I found an answer, and it did cover most of the cases out there, I just had to change prepare(for:sender) in the first view controller to be like this one
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let destVc = segue.destination as? Order,
let index = storesTV.indexPathForSelectedRow?.row
else{return}
destVc.stores = stores[index]
}
the data get passed successfully to the second VC (Order), and I wright another prepare(for: sender) in Order VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVc = segue.destination as? GoogleMap{
destVc.stores = stores
}
}
and this is an image shows that the data get passed to the third VC (GoogleMap),
the second and the third Vc they all comes from one navigationController,
you can find the answer in this article right here
https://matteomanferdini.com/how-ios-view-controllers-communicate-with-each-other/
So I am trying to make an Painting app that can store the data that user gives (such as name of the painting, artist of the painting, year of the painting and image of the painting) and shows in a table view. I have 2 view controllers, first one is called ViewController that has a table view for showing the data (only name of the painting for the table view cell) and the second one is called DetailedVC which is for entering and saving the data, also showing the details of the data. In the second view controller I added 3 text fields, 1 image view that enables the user to go the photo library and a save button. I wrote this in my DetailedVC script for when the user taps one of the elements from the table view in my ViewController:
override func viewDidLoad() {
super.viewDidLoad()
if chosenPainting != "" {
savebutton.isHidden = true //Trying to hide the save button here!!
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "Paintings")
let idString = chosenPaintingId?.uuidString
fetch.predicate = NSPredicate(format: "id = %#", idString!)
fetch.returnsObjectsAsFaults = false
do {
let results = try context.fetch(fetch)
if results.count > 0 {
for result in results as! [NSManagedObject] {
if let name = result.value(forKey: "name") as? String {
nameText.text = name
}
if let artist = result.value(forKey: "artist") as? String {
artistText.text = artist
}
if let year = result.value(forKey: "year") as? Int {
yearText.text = String(year)
}
if let imageData = result.value(forKey: "image") as? Data {
let imageD = UIImage(data: imageData)
imageView.image = imageD
}
}
}
} catch {
print("error")
}
} else {
savebutton.isEnabled = false
nameText.text = ""
artistText.text = ""
yearText.text = ""
}
I get the information from my ViewController like this:
#objc func AddButtonTapped(){
selectedPainting = ""
performSegue(withIdentifier: "toDetailedVC", sender: self)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedPaintingId = ids[indexPath.row]
selectedPainting = names[indexPath.row]
performSegue(withIdentifier: "toDetailedVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetailedVC" {
let destination = segue.destination as! DetailedVC
destination.chosenPainting = selectedPainting
destination.chosenPaintingId = selectedPaintingId
}
}
It works just fine below the savebutton.isHidden = true. I don't understand why the button is not disappearing. Everything else works correctly, text field's texts turn into the information that user gave but, the button is standing still right in the bottom. And even the savebutton.isEnabled = false is working. I thought there must be a problem with connection between my script and my storyboard so I deleted and did it again, but it didn't work again. I must have doing something wrong, can you help me about this?
my code before the migation to Swift 2.0:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "RhymeFavoriten") {
// pass data to next view
let dest = segue.destinationViewController as! FavoritenViewController
let source = segue.sourceViewController as! RhymeViewController // !!!!!!
dest.favoritenType = 1
dest.delegate = self
}
}
the migration told me to change it to
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "RhymeFavoriten") {
// pass data to next view
let dest = segue.destinationViewController as! FavoritenViewController
_ = segue.sourceViewController as! RhymeViewController // !!!!!!!!!!
dest.favoritenType = 1
dest.delegate = self
}
or
func textSelected(selectedText:String, selectedType:Int) {
var fullTextArr = text.componentsSeparatedByString("\n")
var myArray = [String]() // !!!!!!
to
func textSelected(selectedText:String, selectedType:Int) {
var fullTextArr = text.componentsSeparatedByString("\n")
_ = [String]() // !!!!!!!!!
I can´t see, what is _ = standing for :-(
_ is a placeholder. It means that the values assigned to _ are ignored.
Xcode's migration tool made this changes because it has detected that you didn't use source or myArray anywhere, thus replaced these variables by the placeholder.
Now instead of being assigned to a variable, the returning result of segue.sourceViewController as! RhymeViewController and the returning result of [String]() are ignored.
The returning result is ignored but the expression is still evaluated at runtime: if it has side effects, these effects will occur.
So if you actually don't need these instructions you should get rid of them entirely.
I have a UITableView that uses an NSFetchedResultsController to fetch my data for the UITableView. When a user selects a row in the UITableView, I have an edit modal view slide up with two UITextFields for the user to edit the data that was in the UITableViewCell. What I am trying to accomplish is setting the text of each UITextField with the text that was in the UITableViewCell from Core data. I am doing this so that the user does not have to re-type their data just to make an edit.
The issue is when I try to set the text for each UITextField in prepareForSegue, I am getting a fatal error " unexpectedly found nil while unwrapping an Optional value". I know that the data is there, so I am unsure exactly why it is finding nil. I followed this stackoverflow post to write my prepareForSegue function correctly when using an NSFetchedResultsController. This is my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "edit" {
if let inputController = segue.destinationViewController as? QAInputViewController {
let uri = currentDeck!.objectID.URIRepresentation()
let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
inputController.questionInput.text = data?.question
inputController.answerInput.text = data?.answer
}
}
}
}
Thank you for your help.
I found that my the crash was happening because my destination view controller's view was not loaded yet when trying to assign text to each UITextField.
So I added two variables of type String in my destination view controller to hold the data from my prepareForSegue function instead of trying to assign the text from the selected UITableView row directly to the UITextField and then in viewWillAppear I assigned those variables to each UITextField.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "edit" {
if let inputController = segue.destinationViewController as? QAInputViewController {
let uri = currentDeck!.objectID.URIRepresentation()
let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
inputController.selectedQuestion = data!.question
inputController.selectedAnswer = data!.answer
}
}
}
}
In my destination view controller:
var selectedQuestion: String = ""
var selectedAnswer: String = ""
override func viewWillAppear(animated: Bool) {
questionInput.text = selectedQuestion
answerInput.text = selectedAnswer
}
I have a managedObject that is being passed from 1 view controller to another the first pass works fine but when I try to pass the next object after the relationship has been set it doesn't send anything and comes back as either nil or if I try to use other methods comes back with a syntax error. The code I am using for the view controllers is as follows
View Controller 1, The first object set:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let identifier = segue.identifier {
switch identifier {
case "popOver":
if let VC = segue.destinationViewController as? ClassDeckNameViewController
{
if let ppc = VC.popoverPresentationController {
VC.modalPresentationStyle = UIModalPresentationStyle.Popover
ppc.permittedArrowDirections = UIPopoverArrowDirection.Any
ppc.delegate = self
}
VC.classSave = (sender as! ClassSelection)
}
default: break
}
}
}
#IBAction func buttonPriest(sender: AnyObject) {
let entity = NSEntityDescription.entityForName("ClassSelection", inManagedObjectContext: classMOC!)
let newObject = ClassSelection(entity: entity!,insertIntoManagedObjectContext: classMOC)
newObject.classname = "Priest"
var error: NSError?
if let err = error {
println(err)
} else {
classMOC?.save(&error)
self.performSegueWithIdentifier("popOver", sender: newObject)
}
}
This passes the object without problem to the second view controller but this is the one that won't pass any further to the final presenting controller offering the user the final selections for their "Deck":
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showCardSelection" {
let detailVC: CardSelectionViewController = segue.destinationViewController as! CardSelectionViewController
detailVC.passedDeckObject = (sender as! Deck)
}
}
#IBAction func enterButton(sender: AnyObject) {
let entityDescription = NSEntityDescription.entityForName("Deck",inManagedObjectContext: managedObjectContext!)
let storeDeck = Deck(entity: entityDescription!,insertIntoManagedObjectContext: managedObjectContext)
storeDeck.deckname = usersDeckName.text
storeDeck.classSelected = classSave!
var error: NSError?
managedObjectContext?.save(&error)
if let err = error {
status.text = err.localizedFailureReason
} else {
usersDeckName.text = ""
status.text = "Deck Saved"
self.performSegueWithIdentifier("showCardSelection", sender: storeDeck)
}
}
I made passedDeckObject a variable of type Deck? in the final view controller to set the final relationship methods I know I am doing something wrong but I am unsure what! Any help with this would be amazing!
This looks to be a misconfiguration issue where the segue is being triggered directly in the storyboard rather than calling your code. As such the sender is a button rather than the new entity instance you're expecting.
To fix, disconnect the segue in the storyboard and connect (if it isn't already) the button to your action method in the view controller.