Passing Core Data objects from UITableViewCell to another View Controller - swift

Currently I have the application loading up the data in a tableview presented to the user that I want them to select the object they wish to view further details about. From their I want to be able to read out the data in the object to the user in various forms either labels or a tableview dependent on the data.
I have been reading around and trying to understand this stuff but it is all still new to me! I had another guy on here help me out with saving the data and passing the data around to store but reading it out is a seemingly different thing. Any examples in swift language would lead me down the right path I know that I need to use the didselectrow method and call a segue and also that I need a prepare for segue but I am not quite sure how it should look.
I have read multiple posts and some do actually attempt to pass objects but not in the manner in which I am trying to..Are you able to pass whole objects after they have been have been selected in a tableview to another view controller to present all data related to that object or are you only able to pass information from that object to the next viewcontroller? I have examples of prepareforsegue and perform segue before not sure what else I am missing but I am currently not able to pass any information between the tableview and the viewcontroller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "decktoRecord") {
var detailVC: StatWidgetViewController = segue.destinationViewController as! StatWidgetViewController
detailVC.deckRecord = decktoPass
}
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let indexPath = tableView.indexPathForSelectedRow()
let selectedCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell?
self.performSegueWithIdentifier("decktoRecord", sender: indexPath);
}
decktoPass is just a variable that contains the type of data entity that is the primary entity of the object.

A little confused by what you're asking but from what I understand, once the user clicks on a cell you want to segue to another view controller where they can edit the the details?
To add an exception breakpoint, open up your left panel with all your files/viewcontrollers, at the very top of it should be a small panel with a few icons, the first one is a folder, click on the second last one (the one that looks like a tag)
click on the plus in the bottom right and click add exception breakpoint, this should let you know where the problem in your code is occurring
Okay to edit the details in another View controller start by preparing the segue from the original view controller
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showTaskDetail" {
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
let indexPath = self.tableView.indexPathForSelectedRow()
let thisTask = fetchedResultsController.objectAtIndexPath(indexPath!) as! TaskModel
detailVC.detailTaskModel = thisTask
detailVC.delegate = self
}
else if segue.identifier == "showTaskAdd" {
let addTaskVC:AddTaskViewController = segue.destinationViewController as! AddTaskViewController
addTaskVC.delegate = self
}
}
okay so like the code is showing above, I have a segue called showTaskDetail which shows the details of whatever it is, in my case its a simple task. You said you want to edit this information in another view controller when a user clicks on the row, well you need to be able to get this information in the other view controller.
So create a variable in the other viewcontroller that will hold these values, for me i called it detailTaskModel
var detailTaskModel: TaskModel!
Incase you're confused about what TaskModel is, I'm using CoreData to store my data, and TaskModel is a NSMangedObject class.
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
this line you're just specifying what your other view controller is, replace TaskDetailViewController with your swift class.
let detailVC: TaskDetailViewController = segue.destinationViewController as! TaskDetailViewController
this line fetches the data from the row selected
Now you should be able to pass your information into the other view controller and edit it using the detailTaskModel, do you need help with that as well?

Related

How To Pass Dictionary To Another Dictionary In A Different View Controller

I have two view controllers (autoClaimViewController and reviewAutoViewController). In autoClaimViewController, I have a dictionary (reviewTableViewData) that is made up of a struct called claimData (it contains two string variables and a UIImage variable). When the user hits the "Review" button, I want the reviewTableViewData dictionary to be passed to the second view controller so that it's data can be displayed on a table view in the second view controller (reviewAutoViewController). How do I pass this dictionary to the other view controller?
Please make your answers understandable for a beginner - I'm still learning. I'm moving between view controllers using storyboard segues.
Thanks.
Additional Question: Will the images that I stored in the variables be passed when the dictionary is passed? In other words, do images work like Integers and Strings, where they can be passed between variables without an issue?
My code:
struct claimData {
var images = UIImage()
var imageTitle = String()
var relatedUnrelated = String()
}
class autoClaimViewController: UIViewController {
var reviewTableViewData = [claimData]()
#IBAction func reviewButton(_ sender: Any) {
//PASSES THE INFORMATION TO THE REVIEW VIEW CONTROLLER
//FIX THIS... IT NEEDS TO SEND THE reviewTableViewData ARRAY.
//MAKE A DICTIONARY IN THE REVIEW CONTROLLER THAT RECEIVES THE DICTIONARY FROM THIS VIEW CONTROLLER.
let vc2 = reviewAutoViewController()
//Find out how to transfer a dictionary from one view controller to another
}
I think your best bet is to set up a segue in your storyboards which takes you from 1st view to the second view. Here is a guide.
https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html
In both views have a place to store the information you want to pass. So create a var in the secon view to hold reviewTableViewData from the first view.
Then in the 1st view you call
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.destination is reviewAutoViewController
{
let vc = segue.destination as? TertiaryViewController
vc?.reviewTableViewData = reviewTableViewData
}
}
This get the data ready to be sent.
Then you perform the segue and the data should be passed for you. Perfomr the seugue with this func triggered by the button or whatever you use to tranisiton between views.
func performSegue(withIdentifier identifier: String,
sender: Any?){
enter code here
}

Cannot pull data between view controllers: Swift 3

I have a view controller with a name input text box in it. I want to be able to pull the name.text value from my main view controller, but I'm struggling with the code.
Sorry, I know there are similar questions, I am struggling to understand where I am going wrong though, here is my code:
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "statsegue" {
let statVC = segue.destination as! SecondViewController
NameLabelMain.text = statVC.NameDisplayStat.text
//or try that if above doesn't work: detailsVC.passedString = recipes[indexPath?row]
}
}
This is on my main page, trying to pull the data from my second view.
Any thoughts or suggestions would be awesome.
Thanks
This is a common timing problem.
In prepareForSegue the view of the destination view controller is not loaded yet so all IBOutlets are not connected yet (aka not available).
You can only access data which is available right after initialization.
However generally you are passing data from the source controller to the destination controller rather than the other way round.

Is it possible to have 2 times prepare for segue for 2 different view controllers?

I am swift beginner and I am struggling with my first application. Sorry if my question is too basic, but I get an error message in my code here:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let secVC: NavigationViewController = segue.destinationViewController as! NavigationViewController // here is the error
secVC.receiveImeNaSladkarnica = Label1.text!
secVC.KordaA = Shirina.text!
secVC.KordaB = Duljina.text!
secVC.PodImeNaObekt = Label2.text!
I have created 2 different buttons: Navi Me and More Info buttons.
The first one (Navi Me) is connected with another view controller which is called NavigationViewContorller, there I send all of the data I need by using prepare for segue. Everything was working perfect, but now I have created another button which is called (MoreInfo).It is connected with 3rd viewcontroller called MoreInfoViewController. I think I have connected everything fine, but still when I click on the MoreInfo button the app stops working and I get the following error: Thread 1: signal SIGBART in the marked line. I cannot understand why is it breaking when this segue is for another view controller, nt for the MoreInfoViewController. Would you please advice me what to do or provide me with an example how to fix it
I have tried that:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
switch segue.identifier {
//error here-> case "VC1" :
let secVC: NavigationViewController = segue.destinationViewController as! NavigationViewController
secVC.receiveImeNaSladkarnica = Label1.text!
secVC.KordaA = Shirina.text!
secVC.KordaB = Duljina.text!
secVC.PodImeNaObekt = Label2.text!
//error here -> case "VC2" :
let secVC2: MoreInfoViewController = segue.destinationViewController as! MoreInfoViewController
secVC2.receiveImeNaSladkarnica = Label1.text!
secVC2.KordaA = Shirina.text!
secVC2.KordaB = Duljina.text!
secVC2.PodImeNaObekt = Label2.text!
default:
break
It still doesn't work
Each segue is supposed to have an unique string identifier.
For multiple view controllers use a switch statement
switch segue.identifier {
case "vc1" :
// prepare to go to vc1
case "vc2" :
// prepare to go to vc2
default : break
}
Once again my yesterday comment:
Since you are a beginner in Swift please learn first to consider the naming convention that variable names start with a lowercase letter.

Tableview passing same data to another Tableview

I have a an app that displays players through a Tableview. The user can "add" a new player and input his name, height, weight, picture, etc... and once saved will be added to the Tableview. They can also tap on each player and it takes them to the player detail view controller where it displays all of the information you entered (name, height, weight, throw, bat, position, etc...). This is the current code when a user taps on a player to take them to the player detail page:
var player2 = [Player]()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "detail" {
let playerDetailScreen = segue.destinationViewController as! PlayerDetailViewController
if let selectedPlayerCell = sender as? PlayerListTableViewCell {
let indexPath = tableView.indexPathForCell(selectedPlayerCell)!
let selectedPlayer = player2[indexPath.row]
playerDetailScreen.playereDetail = selectedPlayer
}
}
}
I am trying to implement a tableview inside the view controller where the user adds a new player and enters the information. This table view is where a user can add the teams or years someone has played (I am using table view because the number of entries can vary). My question is what is the best way to go about this?? Right now I have a 2nd class named Teams.swift for this specific table view but is this the best way to do it? The idea is to then display this data in a tableview on the detail page (again, because the entries will vary in number) so how do I pass the data from one tableview to another??
Should I eliminate the [Teams] class and just put it under the [Player] class? Any thoughts would be appreciated!
Firstly, I would recommend using :
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// what happens when user selects a cell
let cell = self.tableView.cellForRowAtIndexPath.indexPath // This will give you cell that was selected
let selectedPlayer = cell.player // This gives you the instance of the player (Benefit of using player class)
self.performSegueWithIdentifier("identifier", sender : selectedPlayer)
// ^ This will automatically call prepareforseque method.
}
Next, inside prepareforsegue method, do this :
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "detail" {
let playerDetailScreen = segue.destinationViewController as! PlayerDetailViewController
let selectedPlayer = sender as! Player
playerDetailScreen.name = self.selectedPlayer.name
playerDetailScreen.height = self.selectedPlayer.height
... so on.
// So, remember to create property for each name, height, etc.
// inside playerDetailScreen
// This is how you can transfer data
}
}
}

How do I pass a value from my SplitView controller to a map?

So I have a SplitView controller and the cells have cities and states (ex: Dalllas, TX, etc) when I select a cell I know how to go to a detail view using a segue. But, how do I send data another UIViewController containing a map?
After I’ve created my segue my code associated with the SplitView looks like this:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let cell:UITableViewCell = sender as! UITableViewCell
let destinationViewController:ViewController = segue.destinationViewController as! ViewController
// “cell.textLable” is where the value is passed from the SplitView
if let text = cell.textLabel!.text {
let incedent = LocationIncedent(sName: text)
destinationViewController.detailString = "Lat:" + String(stringInterpolationSegment: incedent.incLat) + " Long:" + String(stringInterpolationSegment: incedent.incLong)
DestinationViewController is where the label accepts displays the value. However, how do I get the values sent to a map instead of a label. Moreover, I need to pass latitude and longitude coordinates to a CLLocationCoordinate2D method.
Thanks
You create a property on the destinationViewController for each object you want to pass and assign this property in the segue method.
In your destinationViewController's viewDidLoad() you can user the data signed to these properties to populate a MKMapView...