Get double variable when sent variable to next viewcontroller swift - swift

I create global variable just after import statement:
var kontenid = ""
var judulkonten = ""
then I sent to FreeTiles view controller (other view controller) through tableview and prepareForSegue:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//let cell = tableView.dequeueReusableCellWithIdentifier(reuseContentFreeIdentifier, forIndexPath: indexPath) as! FreeTableViewCell
var contentku = contents[indexPath.row] as ContentModel
kontenid = contentku.id
judulkonten = contentku.title
performSegueWithIdentifier("lemparkonten", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if (segue.identifier == "lemparkonten") {
var svc = segue.destinationViewController as! FreeTiles;
svc.idcontent = kontenid
svc.namacontent = judulkonten
}
}
And on FreeTiles view controller, I put this inside class:
var idcontent :String!
var namacontent :String!
But when I println on FreeTiles view controller:
println("konten id nya:\(idcontent)")
println("judul nya:\(namacontent)")
I got two log of idcontent and two log of namacontent, the first is empty and the second filled with correct idcontent and namacontent.
How to avoid get two threw variable result when sent variable between two view controller? What is the correct code to get only one result for each threw variable?
Edited:
it seems like cache on xcode log because when I change println, it show only one log.
if idcontent.isEmpty && namacontent.isEmpty {
//println("Nothing to see here")
}else{
var content_id = idcontent
var content_name = namacontent
println("content_id:\(content_id)")
println("content_name:\(content_name)")
}
So, I make filter on FreeTiles (second view controller) to get the filled variable.
Regards.

First of all, if you are declaring global variables then you need to specify it's scope. Let's say:
public var kontenid = ""
public var judulkonten = ""
Further, once you have declared global variables you can access it from anywhere and it will also reflect when you will change the value.
So, after assigning values to the variables
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
//let cell = tableView.dequeueReusableCellWithIdentifier(reuseContentFreeIdentifier, forIndexPath: indexPath) as! FreeTableViewCell
var contentku = contents[indexPath.row] as ContentModel
kontenid = contentku.id
judulkonten = contentku.title
performSegueWithIdentifier("lemparkonten", sender: self)
}
In your prepareforsegue method, you don't need to assign the values:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if (segue.identifier == "lemparkonten") {
var svc = segue.destinationViewController as! FreeTiles;
}
}
Now you can access the above global variables anywhere so you can directly use it in other class:
if idcontent.isEmpty && namacontent.isEmpty {
//println("Nothing to see here")
}else{
var content_id = kontenid
var content_name = judulkonten
println("content_id:\(content_id)")
println("content_name:\(content_name)")
}
}
Let me know, if you have any issues against this code.

You are printing your variables 2 times out with your code:
override func viewDidLoad() {
super.viewDidLoad()
println("konten id nya:(idcontent)") // first
println("judul nya:(namacontent)") // second
if idcontent.isEmpty && namacontent.isEmpty { //println("Nothing to see here")
} else {
var content_id = idcontent var content_name = namacontent
println("content_id: (content_id)") // 3
println("content_name:(content_name)") // 4
}}
Your Code seems doing well to send data through segues. Otherwise you could check my answer here:
Sending data with Segue with Swift

Related

Unable to pass on or retrieve data to another class

Trying to create a custom drop-down and facing issues in passing on data and retrieving the user selection from the custom drop-down. The solution however works when I use static var.
In the below code, I have a viewController class from where I am programmatically calling a popOver.
let countryDropDown = customDropdown()
#IBAction func btMultiCountry(_ sender: Any) {
ViewController.countryDropDown.removeAll() //fuction to clear the array before load
for c in g.country{ //Array from which all items are loaded on the custom dropdown
countryDropDown.addItems(labelText: c.countryName!, toggleState: 1) //passing the values in the add function to update the Array
}
countryDropDown.showDropdown(btMultiCountry) //programatically calling the function to display popover that has the collection view with the countries as items.
}
class customDropdown: NSViewController {
#IBOutlet weak var cvDropDown: NSCollectionView!
var pop = NSPopover() // if I put static var, the popover closes with the OK button as required, however, without static the OK button doest do anything
var dropDownLabelText = [String]() //if I put static var, the values are shown on the collection view without any issue. Without static the array is blank that was load from additems function
var dropDownToggle = [Int]() // same as above
var selectedItemsIndex: [Int] {
var a = [Int]()
for (i,t) in dropDownToggle.enumerated() {
if t == 1 {
a.append(i)
print(a)
}
}
return a
}
func showDropdown(_ sender: NSButton){
let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
let VC = storyboard.instantiateController(withIdentifier: "dropDownForm") as? customDropdown
pop.contentViewController = VC
pop.behavior = NSPopover.Behavior.transient
pop.show(relativeTo: sender.bounds, of: sender, preferredEdge: NSRectEdge.minY)
print(pop)
}
#IBAction func btOK(_ sender: Any) { //this function does not close the popover if there is not static mention above
pop.close()
}
I tried putting static when declaring the variables and it worked. I do not want static and want to create different instances of the class that gives me selectedItemsIndex
For eg:
let countryDropDown = customDropdown()
let personDropDown = customDropdown()
I should be able to get a different values for countryDropDown.selectedItemsIndex and personDropDown.selectedItemsIndex
Any help is appreciated.

fatal errors with optionals not making sense

I keep getting a fatal error saying how a value was unwrapped and it was nil and I don't understand how. When I instantiate a view controller with specific variables they all show up, but when I perform a segue to the exact VC, the values don't show up.
Take these functions for example...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if let displayVC = storyboard?.instantiateViewController(withIdentifier: Constants.Storyboards.TeachStoryboardID) as? SchoolEventDetailsViewController {
displayVC.selectedEventName = events[indexPath.row].eventName
displayVC.selectedEventDate = documentsDate[indexPath.row].eventDate
displayVC.selectedEventCost = documentsCost[indexPath.row].eventCost
displayVC.selectedEventGrade = documentsGrade[indexPath.row].eventGrade
displayVC.selectedEventDocID = documentsID[indexPath.row]?.docID
navigationController?.pushViewController(displayVC, animated: true)
}
}
This combined with this function :
func verifyInstantiation() {
if let dateToLoad = selectedEventDate {
dateEditableTextF.text = dateToLoad
}
if let costToLoad = selectedEventCost {
costEditableTextF.text = costToLoad
}
if let gradesToLoad = selectedEventGrade {
gradesEditableTextF.text = gradesToLoad
}
if let docIDtoLoad = selectedEventDocID {
docIDUneditableTextF.text = docIDtoLoad
}
if let eventNameToLoad = selectedEventName {
eventNameEditableTextF.text = eventNameToLoad
}
}
Helps load the data perfectly, but when I try to perform a segue from a search controller the data is not there.
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = selectedEventName
I set the title of the vc to have the event name , and I also recently added a text field to store it as well for experimental purposes (this question).
Now the issue is I want to do a data transfer from an Algolia Search Controller to that VC and I got all the other fields to show up, except for one and that was the document ID. So I created a completion handler function to get the document ID as a string and have it inserted into the vc when the segue is performed, just like how it's there when the vc is instantiated.
Here is the function :
func getTheEventDocID(completion: #escaping ((String?) -> ())) {
documentListener = db.collection(Constants.Firebase.schoolCollectionName).whereField("event_name", isEqualTo: selectedEventName ?? navigationItem.title).addSnapshotListener(includeMetadataChanges: true) { (querySnapshot, error) in
if let error = error {
print("There was an error fetching the documents: \(error)")
} else {
self.documentsID = querySnapshot!.documents.map { document in
return EventDocID(docID: (document.documentID) as! String)
}
let fixedID = "\(self.documentsID)"
let substrings = fixedID.dropFirst(22).dropLast(3)
let realString = String(substrings)
completion(realString)
}
}
}
I thought either selectedEventName or navigationItem.title would get the job done and provide the value when I used the function in the data transfer function which I will show now :
//MARK: - Data Transfer From Algolia Search to School Event Details
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
otherVC.getTheEventDocID { (eventdocid) in
if let id = eventdocid {
if segue.identifier == Constants.Segues.fromSearchToSchoolEventDetails {
let vc = segue.destination as! SchoolEventDetailsViewController
vc.selectedEventName = self.nameTheEvent
vc.selectedEventDate = self.dateTheEvent
vc.selectedEventCost = self.costTheEvent
vc.selectedEventGrade = self.gradeTheEvent
vc.selectedEventDocID = id
}
}
}
}
But it ends up showing nothing when a search result is clicked which is pretty upsetting, I can't understand why they're both empty values when I declared them in the SchoolEventDetailsVC. I tried to force unwrap selectedEventName and it crashes saying there's a nil value and I can't figure out why. There's actually a lot more to the question but I just tried to keep it short so people will actually attempt to read it and help since nobody ever reads the questions I post, so yeah thanks in advance.
I'm a litte confused what the otherVC is, which sets a property of itself in the getTheEventDocID, whilste in the closure you set the properties of self, which is a different controller. But never mind, I hope you know what you are doing.
Since getTheEventDocID runs asynchronously, the view will be loaded and displayed before the data is available. Therefore, viewDidLoad does not see the actual data, but something that soon will be outdated.
So, you need to inform the details view controller that new data is available, and refresh it's user interface. Something like
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
otherVC.getTheEventDocID { (eventdocid) in
if let id = eventdocid {
if segue.identifier == Constants.Segues.fromSearchToSchoolEventDetails {
let vc = segue.destination as! SchoolEventDetailsViewController
vc.selectedEventName = self.nameTheEvent
vc.selectedEventDate = self.dateTheEvent
vc.selectedEventCost = self.costTheEvent
vc.selectedEventGrade = self.gradeTheEvent
vc.selectedEventDocID = id
vc.updateUI()
}
}
}
}
and in the destination view controller:
class SchoolEventDetailsViewController ... {
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI () {
navigationItem.title = selectedEventName
// and so on
}
}
Ok so I decided to attempt a workaround and completely ditched the getTheEventDocID() method because it was just causing me stress. So I decided to ditch Firebase generated document IDS and just use 10 digit generated ids from a function I made. I also figured out how to add that exact same 10 digit id in the Algolia record by just storing the random 10 digit id in a variable and using it in both places. So now instead of using a query call to grab a Firebase generated document ID and have my app crash everytime I click a search result, I basically edited the Struct of the Algolia record and just added an eventDocID property that can be used with hits.hitSource(at: indexPath.row).eventDocID.
And now the same way I added the other fields to the vc by segue data transfer, I can now do the same thing with my document ID because everything is matching :).

Unable to share data between tab view controllers?

I am making an app where I take in user input and display it as a chart, which requires an array of data. I have managed to save data in an array using core data and I cannot figure out how to share that data from one tab to the other TabViewController.
here is how the data is stored and fetched in the FirstViewController
let number = Numbers(context: PersistenceService.context)
number.numberInArray = Int16(numberEnteredInSlider)
PersistenceService.saveContext()
testArray.append(Int(Double(number.numberInArray)))
var numbers = [Numbers]() // Where Numbers = NSManaged Class
var fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Numbers")
do {try numbers = PersistenceService.context.fetch(fetchRequest) as! [Numbers]
for number in numbers {
print(number.numberInArray)
}
}catch {
print("error")
}
and here is the output(printed testarray):
SAVED
2
5
6
5
Now I want to share this test array from one view controller to another(chartsViewController)
this is what I have tried
class chartsViewController: UIViewController {
let mainVC = mainViewController(nibName: "mainViewController", bundle: nil)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print(mainVC.testArray)
updateGraph()
func updateGraph() {
var lineChartEntry = [ChartDataEntry]() //this is the Array that will eventually be displayed on the graph.
for i in 0..<mainVC.testArray.count {
//
let value = ChartDataEntry(x: Double(i), y: Double(mainVC.testArray[i]))
// here we set the X and Y status in a data chart entry
lineChartEntry.append(value)
// here we add it to the data set
}}
//only showing the part needed. I have tried the same solution with another array and it worked.
}
and the output comes as [0]
I have also tried making a singleton but that didn't work out.
To pass data between tabs on UITabBarController /tabBar, what needs to be done is to have an intermediate. (This is usually the main UITabBarController)
Pic of UITabBarController and the child tabbar
Create a Class and link it to this TabBarController within IB
class BaseTBController: UITabBarController {
// Provide the variable which we want to pass
var workoutTitle: String = "Select a Workout"
override func viewDidLoad() {
super.viewDidLoad()
}
}
Assuming you want to pass data from TabBar2 to TabBar1, then on TabBar2 (in this case, I have it as a UITableView). In the delegate method:
extension VCLibrary: UITableViewDelegate{
// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// prepare to store the data to be passed to another TabBar
let tabbar = tabBarController as! BaseTBController
tabbar.workoutTitle = jsonErgWorkouts[indexPath.row].title
// Automatically select Tab1 after choosing
self.tabBarController?.selectedIndex = 0
// Deselect the selected row once we move to Tab1
tableView.deselectRow(at: indexPath, animated: true)
}
}
After selecting the data to be passed, the code (above) will automatically switch to Tab1. Within Tab1, the following code is aimed to receive the passed data
override func viewDidAppear(_ animated: Bool) {
// Obtain Passed in values from BaseTBController
let tabbar = tabBarController as! BaseTBController
// populate the Title as passed from Tab2
workoutTitleLabel.text = tabbar.workoutTitle
}
I learned this from:
https://www.youtube.com/watch?v=GL8-eM93EvQ

Swift, segue information passed won't show

I'm trying to get some information to pass to another view controller. I did the segue and nothing is showing. I'm using an external class to organize the information. But I'm not sure why it's not working.
first view controller:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toLocationVC" {
let lVC = segue.destinationViewController as! LocationViewController
lVC.locationImage?.image = locations[locationSelection].image;
lVC.nameLabel?.text = locations[locationSelection].name;
lVC.descriptionTextView?.text = locations[locationSelection].desc;
}
second view:
var selectedLocation : Location?;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationImage.image = selectedLocation!.image
nameLabel.text = selectedLocation!.name
descriptionTextView.text = selectedLocation!.desc
}
and this is the class Location:
class Location {
var image : UIImage
var name : String
private var description : String
var desc : String {
return description + "\n\n\nThis Description and Images Provided by http://www.travel.usnews.com"
}
init(name : String, image : UIImage, description: String) {
self.name = name;
self.image = image;
self.description = description;
}
}
I've tried changing some of the code around, but nothing seems to work.
Obviously you override the information you just set prepareForSegue() in your second view controller's viewDidLoad() method.
Just remove the following code from your viewDidLoad() and it should be working (if this is actually the correct segue and all data are set):
locationImage.image = selectedLocation!.image
nameLabel.text = selectedLocation!.name
descriptionTextView.text = selectedLocation!.desc
(And I got the feeling that the selectedLocation is nil (= not set) in the viewDidLoad().)
I would rewrite the segue as follows:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toLocationVC" {
if let lVC = segue.destinationViewController as? LocationViewController {
lVC.selectedLocation = locations[locationSelection]
}
}
Then set a breakpoint inside the if let lVC = segue...{ block to see if it's ever executed. Step through the code and use the po {variable name here} command in the debugger to look into each variable.
You shouldn't be setting anything in the viewDidLoad() function any longer like Mischa suggested. So delete those assignments.
If this answer doesn't help, I think you'll need to update your code listings to include more information. We can't see exactly where a lot of these variables are declared, or if the second view is the correct class.

Queue of functions - Swift

I have a table in which each cell there is the profile name of a user. I want that when the profile name is clicked, it will go to the profile page of the user. I created a delegate from the cell for this. But the problem is, the variable "goToProfileVar" is empty at the starting point. Then I assign a value to it with the "goToCell103" function. Then the assigned value should be used in the "prepareforsegue" to pass the username data to the profile page. The problem is that, prepareforseque functions works first and the "goToProfileVar" becomes empty and passed so, then the variable is assigned from the cell.username.text.
How can I queue these functions?
var goToProfileVar = String()
var goToProfileVar2 = String()
func goToCell103(cell: mainCell)
{
var goToCell103:mainCell? = cell as mainCell
var indexPath: NSIndexPath = self.resultsTable.indexPathForCell(goToCell103!)!
goToProfileVar = cell.usernameLbl.text!
goToProfileVar2 = cell.objectid.text!
println("\(goToProfileVar) first.")
println("\(goToProfileVar2) first.")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "goToProfile8") {
var svc = segue.destinationViewController as! generalProfileLast;
svc.dataPassed = goToProfileVar}
else if (segue.identifier == "goToLikers1") {
var svc = segue.destinationViewController as! likers;
svc.dataPassed = goToProfileVar2}
else if (segue.identifier == "goToComments1") {
var svc = segue.destinationViewController as! enterCommentVC;
svc.dataPassed = goToProfileVar2}
else if (segue.identifier == "goToComments2") {
var svc = segue.destinationViewController as! commenters;
svc.dataPassed = goToProfileVar2}
println("\(goToProfileVar) second.")
println("\(goToProfileVar2) second.")
}
It is in general bad approach to use current view state as a model. Likely your UI populated from somewhere, so use the same source of data to initialize your controllers in prepareForSegue.
If you do not want to change a lot of your architecture and still prefer your way you can use goToCell103 right in prepareForSegue with a sender as an input parameter. In a cell segues sender would be a taped cell.