override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toDetails" {
if let indexPath = sender as? IndexPath {
if let nextVC = segue.destination as? JobDetailViewController {
let valueToPass = jobs[indexPath.row].text <- Thread1
let passUserName = jobs[indexPath.row].addedByUser
nextVC.jobDetail.text = valueToPass
nextVC.userLabel.text = passUserName
}
}
}
}
EDIT: I'm now getting "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value" as an Error.
This is the code of my DestinationVC
#IBOutlet weak var jobDetail: RoundLabel!
#IBOutlet weak var userLabel: UILabel!
var valueToPass: String = ""
var passUserName: String!
override func viewDidLoad() {
super.viewDidLoad()
jobDetail.text = valueToPass
userLabel.text = passUserName
}
}
Expression resolves to an unused property
This error means, that you wrote code with reference for some property of some item in jobs array, but you haven't done anything with it (declare some constant, change some variable, etc.)
You probably just wanted to declare Job item for specific row, so you can do it like this
let job = jobs[indexPath.row]
Related
I've got a ViewController class, and two IBOutlet UILablels that I created from storyboard.
Here is the code of the class:
import UIKit
class AnnouncementViewController: UIViewController {
#IBOutlet var nameLabel: UILabel!
#IBOutlet var infoTV: UITextView!
var mail: String = ""
var url: String = ""
override func viewDidLoad() {
super.viewDidLoad()
}
func set(_ announcement: [String: String]) {
print(announcement)
nameLabel.text = announcement["name"] // here goes the error
self.infoTV.text = announcement["info"]
self.mail = announcement["email"]!
self.url = announcement["url"]!
}
}
And here is the code from another class (TableViewController) I've got:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
guard segue.identifier == "announcementSegue" else { return }
let indexPath = tableView.indexPathForSelectedRow!
let announcement = tasks[indexPath.row]
let destinationVC = segue.destination as! AnnouncementViewController
destinationVC.set(announcement)
}
The error I get is:
Unexpectedly found nil while implicitly unwrapping an Optional value
So, nameLabel is nil. I cannot get why it is.
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"]!
}
I'm new to ios dev and i'm trying to develop my first app! :)
so far so good...
Now I'm trying to pass data from main controller to the item details view.
It seems i can't figure out what i'm doing wrong but i keep getting an error:
"fatal error: unexpectedly found nil while unwrapping an Optional value"
now i understand that the value is not assigned but i don't understand why...
here's my code:
-------> MainVC
struct Ananlysys {
var descrizione: String!
var data: String!
var immagine: String!
var seganle: String!
var oraUpload: String!
var valuta: String!
}
var analysisList = [Ananlysys]()
var alertsRef = FIRDatabase.database().reference().child("analisi")
override func viewDidLoad() {
super.viewDidLoad()
fetchDataFromDB()
}
func fetchDataFromDB(){
alertsRef.observe(.childAdded, with: { snapshot in
if let dict = snapshot.value as? [String: AnyObject] {
let descrizione = dict["descrizione"] as! String
let data = dict["data"] as! String
let stato = dict["stato"] as! String
let immagine = dict["imageURL"] as! String
let seganle = dict["segnale"] as! String
let oraUpload = dict["oraupload"] as! String
let valuta = dict["valuta"] as! String
self.analysisList.insert(Ananlysys(descrizione: descrizione,
data:data, immagine:immagine, seganle: seganle, oraUpload: oraUpload,
valuta: valuta), at: 0)
self.tableView.reloadData()
}
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ItemDetailVC"{
if let indexPath = self.tableView.indexPathForSelectedRow {
let destination = segue.destination as! ItemDetailVC
let cell = tableView.cellForRow(at: indexPath) as? ItemCell
destination.valuta = (cell?.valutaLabel.text)!
destination.segnale = (cell?.signalLabel.text)!
destination.desc = (cell?.descriptionLabel.text)!
destination.immagine = (cell?.imageThumb.image)!
}
}
And in My ItemDetailsVC controller (which is the destination) i've just created the IBoutlet. Here' the code:
class ItemDetailVC: OpenAlertsVC {
#IBOutlet weak var crossLabel: UILabel!
#IBOutlet weak var signalLbl: UILabel!
#IBOutlet weak var imageDetail: UIImageView!
#IBOutlet weak var analysisDesc: UILabel!
var valuta = ""
var segnale = ""
var immagine: UIImage!
var desc = ""
}
override func viewDidLoad() {
super.viewDidLoad()
crossLabel.text = valuta
signalLbl.text = segnale
analysisDesc.text = desc
imageDetail.image = immagine
}
Probably i'm just doing some stupid error... :) but it's my first app and i really don't know how to figure this out!
If anybody could help that would be much appreciate! :)
Thank you!
Cheers!
UPDATE:
I think my problems is because of the image.
I have an url which i retrive from firebase in this way:
let url = analysisList[indexPath.row].immagine
and then i download async the images:
cell.imageThumb.downloadImageFrom(link: url!, contentMode:
UIViewContentMode.scaleToFill)
in the segue i do this:
destination.immagine = (cell?.imageThumb.image)!
and in my DetailVC:
var immagine: UIImage!
imageDetail.image = immagine
this is the screen of the error
enter image description here
Saw this recently passing variables between views. I was able to get them moving by first setting the value to a top level variable (when a row is selected) and then re-reference that variable during the segue prepare function. Hope this helps.
Set a top level variable:
var valuta: String!
Breakout the self.tableView.indexPathForSelectedRow section into it's own delegate. This tutorial is a great example. (http://shrikar.com/swift-ios-tutorial-uisearchbar-and-uisearchbardelegate)
Using the "didSelectRowAt indexPath" delegate, set the top level variable value.
func tableView(_ tableView: UITableView, didSelectRowAt IndexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)!
valuta = cell?.valutaLabel.text
}
Sample adjustments to the prepare function:
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "ItemDetailVC"{
if let destination =
segue.destination as? name_of_your_next_view_controller {
destination.passedValuta = valuta
}
}
Make sure to put a receiving variable in the next view
var passedValuta: String!
class NewCourseViewController: UIViewController {
#IBOutlet weak var courseNameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "newClassComplete" {
if let vcSendingToo = segue.destination as? CoursesTableViewController {
// Pass courseName to nameOfCourses Array
// Course Name
let courseName = courseNameTextField.text!
vcSendingToo.nameOfCourse = courseName
vcSendingToo.nameOfCourses.append(vcSendingToo.nameOfCourse)
}
class CoursesTableViewController: UITableViewController {
var nameOfCourses = [String]()
var nameOfCourse = ""
}
It restarts to a blank array rather than add a value to the same array. I can't find any examples that are online that'll help resolve my issue. It was working fin then stopped all of a su
Have you tried to change this line
let courseName = courseNameTextField.text!
vcSendingToo.nameOfCourse = courseName
vcSendingToo.nameOfCourses.append(vcSendingToo.nameOfCourse)
to this?
let courseName = courseNameTextField.text ?? "" //validate for nil
vcSendingToo.nameOfCourse = courseName
vcSendingToo.nameOfCourses = [courseName]
I made the variables global and removed the "vcSendingToo" properties.
When trying to pass String variables to a new ViewController, only one of the variables is being sent. I'm not sure what is going on here, because when I print object["eventDescription"], the console does indeed print the description. Only eventName is being passed to the new view controller.
Sending view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "detailSegue") {
if let destination = segue.destinationViewController as? EventDetailViewController {
if let eventIndex = self.tableView.indexPathForCell(sender as! FeedCustomCell) {
var object = self.eventsArray[eventIndex.row]
destination.nameText = object["eventName"] as! String
destination.descriptionText = object["eventDescription"] as! String
print(object["eventDescription"])
The destination view controller (EventDetailViewController):
class EventDetailViewController: UIViewController {
#IBOutlet weak var eventName: UILabel!
#IBOutlet weak var descLabel: UILabel!
var descriptionText = String()
var nameText = String()
override func viewDidLoad() {
super.viewDidLoad()
self.eventName.text = nameText
self.descLabel.text = descriptionText
}
}
Sorry for wasting anybody's time, but I had to place constraints onto the description UILabel in the EventDetailController, and then I could see the description.
I'm trying to pass data from a PFTableViewCell to the next view controller(details) with prepareForSegue function. I think I've put in the right code but when I run the app and click on the cell, my app crashes and I get this message "unexpectedly found nil while unwrapping an Optional value". I've tried many variations for the prepareForSegue function to pass the data but it would never appear on the next view controller.
In my FirstViewController I have this code written:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Detail" {
if let destination = segue.destinationViewController as? ResponseViewController {
let path = tableView?.indexPathForSelectedRow!
let cell = tableView!.cellForRowAtIndexPath(path!) as! Post
destination.name = (cell.name.text!)
destination.message = (cell.message.text!)
}
Here is the code to display the data passed in the ResponseViewController:
var name = String?()
var message = String?()
#IBOutlet weak var userName: UILabel!
#IBOutlet weak var userMessage: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
userName.text! = name!
userMessage.text! = message!
}
Now how do I fix this issue or better yet how can I prepareForSegue in swift 2.0?
One of your Optional variables contains nil.
Using '!' forces unwrapping of Optional values. If an Optional contains nil when it is unwrapped your program crashes with this error message. Try to avoid using force-casting (as!) and force-unwrapping.
Here's a nil safe version
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Detail" {
if let destination = segue.destinationViewController as? ResponseViewController
, path = tableView?.indexPathForSelectedRow
, cell = tableView?.cellForRowAtIndexPath( path ) as? Post
{
destination.name = cell.name.text ?? "nil"
destination.message = cell.message.text ?? "nil"
}
and
var name: String?
var message: String?
#IBOutlet weak var userName: UILabel?
#IBOutlet weak var userMessage: UILabel?
override func viewDidLoad()
{
super.viewDidLoad()
userName?.text = name ?? "nil"
userMessage?.text = message ?? "nil"
}
N.b.:
?? is the nil-coalescing operator. If the value on the left is nil, the value on the right is substituted.
You can optionally call methods on an optional value using the ?. accessor. If the value on the left is nil, the result will short-circuit to nil