Swift: How do I pass data from "didSelectRowAt" to another VC without presenting it or a segue?

Related searches

Hope everyone is doing well!

What I basically want is I want to press on a row in my TableViewController which then takes, for example, the text that the row has and passes it to a ViewController that is currently not present. Like when you click on a song in the Spotify app and it plays it without presenting anything but the song details are shown in the mini-player.

Does anyone have a clue how to do that? Thank you in advance!

Well you can do that but you need to initialize your View Controller first for you to access its properties like:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DestinationVc") as? DestinationVc
    vc?.text = "TextToBePassed"

And there it is without presenting it but I don't get it why you don't want to present or show it but that's how it is done based on your question. Thanks :)

How to pass data between two view controllers, We'll learn how to call a segue and pass data from one view to another with Swift. ▻Get my Duration: 12:38 Posted: Feb 18, 2018 Whenever you want to send data back to viewController1, have viewController2 send the "delegate" the data. Setup a closure as a property that allows passing the data. viewController1 would implement that closure on viewController2 when displaying viewController2. Whenever viewController2 has data to pass back, it would call the closure.

If you have created the UI via XIB or programatically then follow this approach:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

  let nextVC = NextViewController()
  nextVC.text = model?[indexPath.row].text // If you are using model or if you have stored the data in the array then nextVC.text = yourArray[indexPath.row]["text"]    
  self.navigationController?.pushViewController(nextVC,animated: true)


Also, in the NextViewController, you have to add text. Like this:

class NextViewController: UIViewController {

  var text = String()

  //MARK:- View Life Cycle

  override func viewDidLoad() {
     print(text) //This will print the text passed from previous VC

Passing Data between View Controllers (Swift 4), Swift. There are tons and tons of explanations here and around StackOverflow, but if you are a beginner just trying to get something basic to work, try� Welcome Back to a brand new episode of Mastering Programming in this episode I will show you how to Share/Pass Data between View Controllers. Thank you for w

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

      let vc = NextViewController()
        vc.title = "\(Title)"
      self.navigationController?.pushViewController(vc,animated: true)


Using this method can help you in passing the data to another Controller without using segue .

Passing Data between View Controllers, Don't use object parameter to pass data. It is meant to filter notifications with the same name, but from a particular object. So if you pass some object when you post a notification and another object when you addObserver, you won't receive it. If you pass nil, you basically turn off this filter. You should use userInfo parameter instead.

Assuming this is for iOS as the question doesn't specify.

In the tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method in your UITableViewDelegate, you can grab the cell's text with

let cell = tableView.cellForRow(at: indexPath)?.textLabel?.text // cell: String?

You can then assign this to a global variable if you wish, or if you maintain a reference to the non-present view controller, you can give it a property to store this text and assign it before you call performSegue(withIdentifier identifier: String, sender: Any?).

Can I pass an AnyObject to JavaScript and access it? If not, what type should I make the swift class so that I can easily pass it. I am tempted to just create a javascript object in swift as a string and pass that, but I feel there's a better way. Thanks. EDIT: I answered how I was able to pass data as JSON below.

How To: Using Notification Center In Swift. Updated for Swift 5 and iOS 13. With NotificationCenter you can broadcast data from one part of your app to another. It uses the Observer pattern to inform registered observers when a notification comes in, using a central dispatcher called Notification Center.

Pass data to and from Popover in Swift. Tag: ios,swift. In my current project i have detail view that shows a specific record from my table view. I have the following

If you are an iOS developer, or a software developer in general, you definitely solve this problem in almost every project: how to pass data from the Model to Controller. We will go through each of…