skip to Main Content

I come here to ask for help for a small problem that is occurring in the development of this application.

I have little experience with Swift, and I’m writing some code to learn more. I have this application that has in the initial screen a set of buttons that redirects to different pages and features.

The button called "Table View (CH Cantons)" redirects to a new view, which contains a TableView that show us three things:

  • Canton emblem or coat of arms
  • The Canton, as title : Ex: ZH – Zurich
  • The area of specific canton

When a canton is selected the application should forward to a new page and show details about that particular canton, but this is not happening despite my efforts to solve the problem. Is TableView required to be in the Main View / Main Page?

enter image description here

Bellow is the connection with the Navigation controller

enter image description here

The code that generates the Table View

let cantonsData = ["AG - Aargau","BE - Bern","GE - Geneve","GR -  Graubunden","JU - Jura","LU - Lucern","NE - Neuchâtel","SG - Sankt Gallen","TG - Thurgau","TI - Ticino","UR - Uri","VD - Vaud","VS - Valais","ZH - Zurich","AI - Appenzell n Innerrhoden","AR - Appenzell n Ausserrhoden","SH - Schaffhausen","BL - Basel n Landschaft","BS - Basel","SO - Solothurn","FR - Fribourg","ZG - Zug","GL - Glarus","NW - Nidwald","OW - Obwald", "SZ - Schwyz"]
    
    let area = ["Area (km2): 1404 km2","Area (km2): 5,960 km2","Area (km2): 1,792 km2","Area (km2): 7,105 km2","Area (km2): 839 km2","Area (km2): 1,494 km2","Area (km2): 802 km2 ","Area (km2): 2031 km2","Area (km2): 992 km2","Area (km2): 2812 km2","Area (km2): 1077 km2","Area (km2): 3212 km2","Area (km2): 5224 km2","Area (km2): 1729 km2","Area (km2): 172 km2","Area (km2): 243 km2","Area (km2): 298 km2","Area (km2): 518 km2","Area (km2): 37 km2","Area (km2): 790 km2","FR - Fribourg","ZG - Zug","GL - Glarus","NW - Nidwald","OW - Obwald", "SZ - Schwyz"]
    
    override var modalPresentationStyle: UIModalPresentationStyle {
        get { .fullScreen }
        set { assertionFailure("Shouldnt change that 😠") }
    }
    
    override func viewDidLoad() {
        //super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.title = "CH Cantons / CH Kantons"
        tableView.dataSource = self
        tableView.delegate = self
        
        let nibName = UINib(nibName: "TableViewCell", bundle: nil)
        tableView.register(nibName, forCellReuseIdentifier: "tableViewCell")
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cantonsData.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! TableViewCell
        
        cell.commonInit("CH-(indexPath.item+1)", title: cantonsData[indexPath.item], sub: area[indexPath.item])
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 180
    }

function responsible for opening new page when a cell of table view is selected

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let storyboard = UIStoryboard(name: "TableViewCH", bundle: nil)
        if let vc = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController{
            vc.img = UIImage(named: cantonsData[indexPath.item])!
            vc.canton = cantonsData[indexPath.row]
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }

I’ve tried another one but cannot solve the problem

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        tableView.deselectRow(at: indexPath, animated: true)

        performSegue(withIdentifier: "mySegue", sender: cell)
    }

2

Answers


  1. Try this:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            let storyboard = UIStoryboard(name: "TableViewCH", bundle: nil)
            if let vc = storyboard.instantiateViewController(withIdentifier: "DetailViewController") as? DetailViewController{
                vc.img = UIImage(named: cantonsData[indexPath.item])!
                vc.canton = cantonsData[indexPath.row]
                let nav = UINavigationController(rootViewController: vc)
                nav.modalPresentationStyle = .overFullScreen
                self.present(nav, animation: true)
            }
            
        }
    
    Login or Signup to reply.
  2. As other people have mentioned, you’re presenting your screen modally (this means that it acts more as a popover and has no ViewController Stack). To fix this I would try going into the Storyboard file and selecting the NavigationController and selecting presentation near the bottom of the properties, change this to over current context

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search