5

there is a class MenuViewController in which the array recorded in the table is recorded:

import Foundation
import UIKit

class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var menuTableView: UITableView!

let myTitle = ["Помощь", "Информация", "Поддержка"]


override func viewDidLoad() {
    super.viewDidLoad()


    menuTableView.delegate = self
    menuTableView.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myTitle.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = menuTableView.dequeueReusableCell(withIdentifier: "MenuCell") as! MenuTableViewCell
    cell.labelText.text = myTitle[indexPath.row]
    return cell
}
}

What do you need to write in it to go to Info Controller?

aaallleeexxx918
  • 171
  • 3
  • 11

3 Answers3

4

I assume you are using storyboards. First you need to set up some sort of connection between your two viewcontrollers. You can do this by setting up a "segue". You hold ctrl+click drag from the first viewcontroller to the 2nd viewcontroller. It looks like this:

Creating Segue

When you let go of the mouse, you will see this:

Creating Segue 2

Click on Show. Now you will have created a segue. Now you need to name it. So click on the segue that shows up in the storyboard. It is basically just an arrow from the Menu View Controller to the Info View Controller. On the right hand side you will see a place where you can name it (give it an identifier):

Creating Segue 3

Now ... you have done all you needed to do in the storyboard. Now in your actual code in the MenuViewController, you need to associate clicking the tableviewcell. You do this by using the delegate method didSelectRowAt.

I see you have already set up the delegate with this line: menuTableView.delegate = self

That ensures that didSelectRowAt will be called whenever the user taps on a row.

So now what you want to do is write the code to perform the segue:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "YourSegueName", sender: self)
}

Note: Here is more information on interacting with a tableview row: How to detect tableView cell touched or clicked in swift

Here is more information about segues: IOS - How to segue programmatically using swift

There are many more customizations you can do ... such as passing data through to the next viewcontroller via the segues. Here's how: Pass data through segue

Gabriel Pires
  • 926
  • 9
  • 12
2

implement UITableViewDelegate method:

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

       //To InfoViewController I am considering you have created InfoViewController with XIB 
       let controller = InfoViewController(nibName: "YourNibName", bundle: nil)

        //Present Your Controller
        self.present(controller, animated: true) {
        }              

        //Push Your controller if your view is already part of NavigationController stack
        self.navigationController?.pushViewController(controller, animated: true)
    }
Amro Jaber
  • 185
  • 1
  • 17
Jeetendra Kumar
  • 500
  • 3
  • 9
0

Just add a
nextViewController.didMove(toParentViewController: self) inside a "didSelectRowAt". Example given below.

This is for List a tableview cell:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellIdentifier = "cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! PatientFamilyDetailTableViewCell
        cell.txt_patientName.text = patientname[indexPath.row]
        
        // Configure the cell...
        
        return cell
    }

Now it's for Select a Table view cell:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            if cell.isSelected {
                
                
                let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                let nextViewController = storyBoard.instantiateViewController(withIdentifier: "medtestmain") as! medTest
            patient_id = relation[indexPath.row]
            patient_name = patientname[indexPath.row]
            UserDefaults.standard.set(patient_id, forKey: "patient_id")
            UserDefaults.standard.set(patient_name, forKey: "patient_name")
                self.addChildViewController(nextViewController)
                nextViewController.view.frame = self.view.frame
                self.view.addSubview(nextViewController.view)
                nextViewController.didMove(toParentViewController: self)
                
            }
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Gowtham Sooryaraj
  • 3,639
  • 3
  • 13
  • 22