-1

I am trying to get a textfield in the second view controller to show the name of the cell clicked in a first view controller. When using a table view, should I push like below, or present as shown below. This is the current code I have

NearbyViewController

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]


     let viewController = storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController

    viewController.name = cellName

    navigationController?.pushViewController(viewController, animated: true)

    let vct = self.storyboard?.instantiateViewController(withIdentifier: "Home2")
    self.present(vct!, animated: true, completion: nil)


    print("row\(indexPath.row)")
    print("name: \(cellName)")


}

ShopViewController

var name :String?

@IBOutlet weak var myTextField: UITextField!



override func viewDidLoad() {

        print(name)



}
  • 1
    Why are you pushing the vc in navController as well as presenting the vc ? – Nitish Apr 06 '18 at 19:10
  • This is essentially a repost of [your previous question](https://stackoverflow.com/questions/49681324/open-a-viewcontroller-on-cell-click-swift) which has several comments and even an accepted answer. Yet the code in this question doesn't seem to fix any of the issues you were told about in the other question. – rmaddy Apr 06 '18 at 19:11
  • 3
    Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Shobhakar Tiwari Apr 06 '18 at 19:12

5 Answers5

0

You seem to be creating 2 view controllers. instantiateViewController(withIdentifier: "Home2") You change the value first then push, then you present it again. So the controller you are seeing is the second one, which you didn't set the value on.

Remove

let vct = self.storyboard?.instantiateViewController(withIdentifier: "Home2") self.present(vct!, animated: true, completion: nil)

0

Remove these lines :

let vct = self.storyboard?.instantiateViewController(withIdentifier: "Home2")
self.present(vct!, animated: true, completion: nil)  

You are already pushing the same viewController in navigationController.
Your final code should be :

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]

     let viewController = storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
    viewController.name = cellName
    navigationController?.pushViewController(viewController, animated: true)
    print("row\(indexPath.row)")
    print("name: \(cellName)")
}  

This should work only once you have set the viewController's identifier in storyboard. This is how you can do that :

enter image description here

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • I tried this to perform a segue programatically so thats what its for –  Apr 06 '18 at 19:15
  • My dear friend, you are already doing that before. Here : let viewController = storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController – Nitish Apr 06 '18 at 19:16
  • I also tried changing this to perform segue, but it didn't work either –  Apr 06 '18 at 19:17
  • Have you set the vc's identifier in storyboard ? – Nitish Apr 06 '18 at 19:17
  • when I removed the lines, it didn't go to the next page –  Apr 06 '18 at 19:19
  • Where have you defined and declared navigationController ? – Nitish Apr 06 '18 at 19:20
  • How would I go about setting the vc's identifier –  Apr 06 '18 at 19:21
  • With my previous code it opened the new view controller, but just printed the value of name in the new view controller as nil. –  Apr 06 '18 at 19:26
  • Answer these : Are you able to navigate correctly ? And have you set the identifier in storyboard ? – Nitish Apr 06 '18 at 19:28
  • 1
    Remove the needless creation of the cell. And remove the pointless parentheses when declaring cellName. – rmaddy Apr 06 '18 at 20:44
0

I fixed my issue with the following code:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    var (cellName) = myList[indexPath.row]

     let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Home2") as! ShopViewController
    viewController.name = cellName
            self.present(viewController, animated: true, completion: nil)
    navigationController?.pushViewController(viewController, animated: true)


    print("row\(indexPath.row)")
    print("name: \(cellName)")


}
  • Remove the needless creation of the cell. And remove the pointless parentheses when declaring cellName. – rmaddy Apr 06 '18 at 20:45
0

Try this for Objective-c:

    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    SecondViewController *nextView= [story instantiateViewControllerWithIdentifier:@"SecondViewController"];  // Please set "**SecondViewController**" to the secondController's storyboardID from storyboard. 

    nextView.strUserName = @"username" ;   // or you can transfer any type of data array or dictionary.

    [self.navigationController pushViewController:nextView animated:YES];
Nick
  • 875
  • 6
  • 20
-1

The function viewDidLoad is getting called before the name variable is getting set.

Try moving the print() to viewWillAppear instead.

picciano
  • 22,341
  • 9
  • 69
  • 82