Pass data through segue
Swift works exactly the same way as Obj-C but is reworked in the new language. I don't have a lot of information from your post but let's give a name to each TableViewController to help with my explanation.
HomeTableViewController (this is the screenshot you have above)
PlayerTableViewController (this is the player screen you want to travel to)
With that said, in PlayerTableViewController you need to have a variable that will store the passed data. Just under your class declaration have something like this (if you intend to store the struct as a single object rather than the array:
class PlayerTableViewController: UITableViewController {
var programVar : Program?
//the rest of the class methods....
After that there are two ways you can send data to the new TableViewController.
1) Using prepareForSegue
At the bottom of HomeTableViewController you will use the prepareForSegue methods to pass the data. Here is an example of the code you'll use:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
// Create a variable that you want to send
var newProgramVar = Program(category: "Some", name: "Text")
// Create a new variable to store the instance of PlayerTableViewController
let destinationVC = segue.destinationViewController as PlayerTableViewController
destinationVC.programVar = newProgramVar
}
}
Once PlayerTableViewController has loaded the variable will be already set and usable
2) Using didSelectRowAtIndexPath
If specific data needs to be sent based on which cell is selected you can use didSelectRowAtIndexPath. For this to work, you need to give your segue a name in the storyboard view (let me know if you need to know how to do this too).
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Create a variable that you want to send based on the destination view controller
// You can get a reference to the data by using indexPath shown below
let selectedProgram = programy[indexPath.row]
// Create an instance of PlayerTableViewController and pass the variable
let destinationVC = PlayerTableViewController()
destinationVC.programVar = selectedProgram
// Let's assume that the segue name is called playerSegue
// This will perform the segue and pre-load the variable for you to use
destinationVC.performSegueWithIdentifier("playerSegue", sender: self)
}
Let me know if you need any other info on this
Swift 3 - Passing Data from one view to another
If your SearchViewController
is the initial VC
then why would you put a back
button there? Where you want it to Go Back to
? I think it should not be there.
Also unless you really need to do things programmatically in Xcode, you should just use Storyboard
do embed the SearchViewController
inside NavigationController
(select the Controller
then from menu: Editor-> Embed in -> Navigation Controller
).
Then create segue
from controller icon on the top
of the SearchViewController
to the ResultViewController
and you could set identifier for the segue
like you already knew (as you use it in your code).
In your ResultViewController
, if you have did the steps I mentioned above, then you don't need button
for returnToSearch
and you could delete the @IBAction for btnReturnToSearch(_ sender: Any)
. As you will get the <Back
button as Navigation Item
on the top of ResultViewController
to return to SearchVeiwController
for free.
*** Update based on the last comment ****
Okay. I finally understood your issue. In your prepare(for:sender) . you need to . check the Segue Identifier:
if segue.identifier == "CHANGE_ME_WITH_YOUR_SEGUE_FOR_RESULT_IDENTIFIER" {
...
}
before you pass down the search result to the . ResultViewController. As when you segue back, you are not going to the ResultViewController
but the MainViewController
.
Access a variable in another view controller - Xcode 8.0 Swift 3.0
You need to do
hello.whatever your variable name is = the new variable you created on VC2
after you declare the hello variable
hope this helps
override func viewDidAppear(_ animated: Bool) {
print("viewDidAppear")
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("prepare for segue")
let variableUnwind = ("StackOverFlow")
if segue.identifier == "toFirstViewController" {
let hello = segue.destination as! AddItemViewController
hello.variableName = newVariableName
}
}
}
How can I pass data from a VC to another in Swift 3 without a segue?
You can pass data with Navigation in Swift 3 like this: -
Current Class
let editProfile = self.storyboard?.instantiateViewController(withIdentifier: "editProfile") as? EditProfileVc
if detailsModel.count != 0 {
editProfile?.accModel = detailsModel[0] //Passing data here with the array
}
self.navigationController?.pushViewController(editProfile!, animated: true)
and target class (here using model class variable)
class EditProfileVc: BaseNotifyM, PickerDelegate {
// Data Variable
var accModel:AccountModel? //-- Here you can use [String] or [[String: Any]] or as per you choice for data passing
//MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
//Do some thing with your data
print(accModel)
}
}
Swift 3 - Passing data between a View Controller and after that to another 2
Before calling presentViewController
add :
nextViewController.name = yourTextField.text
You could also delete the segue
call. That is redundant.
Here is an example that I've used in the past :
@IBAction func doSegue(_ sender: UIButton) {
buttonTag = sender.tag
let storyboard = UIStoryboard (name: "Main", bundle: nil)
let resultVC = storyboard.instantiateViewController(withIdentifier: "ResultViewController")as! ResultViewController
// Communicate with new VC - These values are stored in the destination
// you can set any value stored in the destination VC here
resultVC.firstValue = buttonTag
resultVC.secondValue = randomOpponentValue()
self.navigationController?.pushViewController(resultVC, animated: true)
}
Related Topics
No Designated Init for Skshapenode(Circleofradius: Radius)
Cloudkit - "Invalid Bundle Id for Container"
Passing Data from Simple Nsview to Swiftui View
How to Combine Two Nsdictionary in Swift
Swift Firebase Check If User Exists
Way to Purge All But One Object Types (Models) in a Realm
How to Resolve Error in Unit Testing When We Have Date Comparison in Codable
Cannot Call Value of Non-Function Type 'Nshttpurlresponse' Alamofire Objectmapper
Apple Push Notifications Without Developer Account
Swift 2: Invalid Conversion from Throwing Function of Type to Non-Throwing Function
How Does Appdelegate.Swift Replace Appdelegate.H and Appdelegate.M in Xcode 6.3
How to Make Prepareforsegue with Uicollectionview
Count the Number of Lines in a Swift String
Characters Is Unavailable' Please Use String Directly
How to Move Platform with Velocity
Swift Delegate Beetween Two Vc Without Segue
How to Specify the Type Information of an Array That Would Hold Swiftui Custom Views