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.
Related
In my HomeClass there is a static var globalLimit: Int = 0 and i have to pass his value in other classes, this is my prepare function
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? SelectClass {
vc.limit = Int(steppeR.value)
HomeClass.globalLimit = Int(steppeR.value)
HomeClass.globalRadiusLimit = Int(stepperRadius.value)
}
else if let np = segue.destination as? CourseClass2 {
np.categories = filteredList
np.numberPlaces = filteredList.count
np.radius = Int(stepperRadius.value)
}
}
now the problem is this, in my CourseClass2 class that i can even reach from SelectClass i have the var numberPlaces = 0 and his value is
override func viewDidLoad() {
super.viewDidLoad()
numberPlaces = HomeClass.globalLimit
}
but like you can see in my prepare function when i come in CourseClass2 directly from HomeClass i want that the value of numberPlaces = filteredList.count but of course it does not work because it goes against it numberPlaces = HomeClass.globalLimit in the viewDidload of CourseClass2, so how can i solve this problem?
Try in this way:
Set your numberPlaces var as optional:
var numberPlaces: Int?
Then check in view did load if it is nil:
override func viewDidLoad() {
super.viewDidLoad()
if numberPlaces == nil {
numberPlaces = HomeClass.globalLimit
}
}
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!
hello I am new to programming. So I have some basic questions and then I need the solution of the problem which I am having.
I have created a Model Class
class Trip: NSObject {
var tripTitle: String
var tripSummary: String
static var trips = [Trip]()
init(tripTitle: String, tripSummary: String) {
self.tripTitle = tripTitle
self.tripSummary = tripSummary
}
class func addTrip(tripTitle: String, tripSummary: String){
let t = Trip(tripTitle: tripTitle, tripSummary: tripSummary)
trips.append(t)
}
}
TripsDetailController
#IBAction func previewButtomPressed(sender: UIButton) {
let tripTitle = tripTitleTxt.text!
let tripSummary = tripSummaryTxt.text!
Trip.addTrip(tripTitle, tripSummary: tripSummary)
// let optionaltrip = Trip(tripTitle: tripTitle, tripSummary: tripSummary)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowTripPreviewTableViewController" {
let tripPreviewTableViewController = segue.destinationViewController as! TripPreviewTableViewController
// how can I pass the object here
}
}
how can I pass the object in segue
tripPreviewTableViewController
In this class I know I have to declare a variable here also but don't know what would be the datatype and also how can I access the value
and also I want to know what would be the best approach from these two
Trip.addTrip(tripTitle, tripSummary: tripSummary)
or
let trip = Trip(tripTitle: tripTitle, tripSummary: tripSummary)
should I have to create an array and assign values or go to the second method of creating an object
Use the destinationViewController:
In the tripPreviewTableViewController you need to add new optional variable type of Trip:
var trip : Trip!
and now you can transfer your object with destinationViewController:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowTripPreviewTableViewController" {
let tripPreviewTableViewController = segue.destinationViewController as! TripPreviewTableViewController
tripPreviewTableViewController.trip = (Your Trip Object)
}
}
//Update
First you need to declare your Trip object in TripsDetailController:
var tripToPass = Trip!
then orient it with one of these ways:
tripToPass = Trip(tripTitle:"title",tripSummary:"summary")
//or get the Trip out of your trips array:
tripToPass = Trip.trips[index]
after that you can pass the trip object:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowTripPreviewTableViewController" {
let tripPreviewTableViewController = segue.destinationViewController as! TripPreviewTableViewController
tripPreviewTableViewController.trip = tripToPass
}
}
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.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "shoesDetails" {
let indexPath = self.table.indexPathForSelectedRow()
let shoesCon:ShoesTableControler = segue.destinationViewController as! ShoesTableControler
let shoesCategories:RepoSitory = arrofRepository[indexPath!.row]
shoesCon.object = shoesCategories
}
}
class ShoesTableControler: UIViewController {
var object = [ProductShoes]()
}
class ProductShoes {
var product_id : String
var product_name : String
init(json :NSDictionary) {
self.product_id = json["product_id"] as! String
self.product_name = json["product_name"] as! String
}
class RepoSitory {
var name : String
var ids : String
init(json :NSDictionary) {
self.name = json["category_name"] as! String
self.ids = json["id"] as! String
}
}
I have 3 classes in firstViewController. We show the data it's done. After that, when user click on specific cell, then show the data. Now I have created the ShoesTableController and in ShoesTableController we create the Product Class obj. I want to access this. The problem is in this line in Objective C. I am doing this and it is working perfect but in swift it does know what happened:
shoesCon.object = shoesCategories
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "shoesDetails" {
let indexPath = self.table.indexPathForSelectedRow()
let shoesCon:ShoesTableControler = segue.destinationViewController as! ShoesTableControler
let shoesCategories:RepoSitory = arrofRepository[indexPath!.row]
shoesCon.object = shoesCategories
}
}
class ShoesTableControler: UIViewController {
var object : RepoSitory?
its solve my problem i have my own mistake sorry for posting this question