Cannot pull data between view controllers: Swift 3 - swift

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.

Related

Navigation controller between two UITableViewControllers in Swift

I am looking for a way to insert a navigation controller between two UITableViewControllers. I have a UITabBarController which then connects to a UITable which then connects to another UITable. I have attached a picture below to give a better reference at what I am talking about.
So from UITabBar, you go to the first table, from that, you make a selection and go to another table. The second table is where I want my back button. I know in the screenshot you can see the back button present but for some reason it does not show it at run time.
Moreover, when I try to embed Navigation Controller in the first table, I get an error in the code below.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let barViewControllers = segue.destination as! UITabBarController
let destinationViewController = barViewControllers.viewControllers?[0] as! MainVC
destinationViewController.TPNumber = txtTpNumber.text!
destinationViewController.intake = intake
let DVC = barViewControllers.viewControllers?[1] as! AttendanceTVC //This is where I get an error
DVC.TPNumber = txtTpNumber.text!
}
If anyone could guide me on how I can solve this? Thanks!
Go to your storyboard and click your Attendance (ViewController), than click your menu "Editor" -> "Embed in" -> UINavigationController.
You can firstly cast this destination as UINavigationController and then you can access the controller embed in UINavigationController by it's topViewController property

Pass data from ViewController (not Detail) to MasterViewController in UISplitView Swift

How to pass data from ViewController (VC) to MasterView (MV) in UISplitView? As I figured out I can't use segue because it goes from VC to SplitView, not to MV.
Check please picture below
screenshot of storyboard
UPD:
Thanks Stepan for the help, I have figured out.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toMessurementVM"{
let splitVC = segue.destinationViewController as! UISplitViewController
let navVC = splitVC.childViewControllers.first as! UINavigationController
let vc = navVC.childViewControllers.first as! MessurementTagsTableView
vc.ingredient = passingIngredient
}
}
I am not sure that this is right way, but it works.
There are many examples of how to pass data from Detail to Master and vice-versa, but there is nothing for my case.
Thanks
1) don't pass data! just use pointers.
2) you can get SplitViewController with superview from MasterView or ViewController
3) register for notification in controller you need and then just send notification that some data ready.
4) on destination controller which catch your notification - just get data with pointer with get superview from controller, cast it to SplitViewController and then you can get master or detail from it with no problems, and then get data from it by pointer.
5) use debugger stop and see what you have in superview, and then You will know exact how to act )
6) make sure to call all UI operation on main thread.

Passing Core Data objects from UITableViewCell to another View Controller

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?

Perform segue with Identifier UIWebView blog, how to link?

Big problem here guys, I'm on the verge of finishing my blog project until I came across a segue issue, I'm trying to display the contents of a blogs web view on a uiwebview in another controller. This is my first time working with MediaRSS and AFNetworking. I have been able to parse the contents before, but am having no luck this time around. All help is greatly appreciated. I keep getting NSIndexPath is not a member of PostLink
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "viewpost" {
let indexPath = tableView.indexPathForSelectedRow()
let item = items[indexPath!.row]
let detailViewController = segue.destinationViewController as PostViewController
detailViewController.postLink = indexPath.postLink
}
}
}
UPDATE 9:17PM
Looks like im performing a segue properly, because now my page will go to a blank. Can someone please confirm the parameters I am passing in, I just need someone to look and tell me please
NSIndexPath does not have any member named postLink. I think you mean to write item.postLink.
detailViewController.postLink = item.postLink
I hope item is of some type that has 'postLink' member and it is populated with appropriate value.

(Swift/XCODE) Creating multiple segues from a single table view controller

I am new to programming in Swift and I having the hardest time trying to figure out how to connect a static table view with 6 cells to 6 separate dynamic prototype views.
From looking at previous responses, I know that I have to identify each segue ("showRestaurant") and I thought it would go in the override segue method but I am not sure. I am also unsure of how to properly code it.
Please let me know if you have any solutions
Thanks so much
Diamond
With a static tableView you can simply ctrl-drag from the tableViewCell to the destination viewController. Choose the type of the segue and give it an identifier (i.e. segueToVC1).
Repeat that for each tableViewCell which should be linked to a viewController.
Then in your custom tableView class file you override prepareForSegue to do additional setup, passing values to the destination viewController etc:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueToVC1" {
// ...
}
}