Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

navigationcontroller - Swift 5; pushViewController is not working

I'm testing Swift and I created a table list to show JSON data, now I would like to show details for each cell, clicking on it, into a new View Controller.

Testing the App I got the click on the cell but I don't see the new View.

This is the code capturing the click into the ViewController.swift:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 
  {
  
  private let tableView: UITableView = {
    let table = UITableView(frame: .zero,
                            style: .grouped)
    table.register(UITableViewCell.self,
                   forCellReuseIdentifier: "cell")
    return table
}()

 private let tableView: UITableView = {
    let table = UITableView(frame: .zero,
                            style: .grouped)
    table.register(UITableViewCell.self,
                   forCellReuseIdentifier: "cell")
    return table
 }()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
   
    parseJSON()
    view.addSubview(tableView)
    tableView.frame = view.bounds
    tableView.delegate = self
    tableView.dataSource = self
    
  }

  ....

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let vc = storyboard?.instantiateViewController(withIdentifier: "Detail") as? DetailViewController {
        vc.selectedImage = "images/logo_2-mini.png"
        print("Click (indexPath)   " + vc.selectedImage!) // I see it!
    
        navigationController?.pushViewController(vc, animated: true) // I don't see it
    }
  }
}

This is the DetailViewController

import UIKit

 class DetailViewController: UIViewController {

 @IBOutlet weak var imageView: UIImageView!
 var selectedImage: String?

 override func viewDidLoad() {
    super.viewDidLoad()
    print("Pre")

    if let imageToLoad = selectedImage{
        imageView.image = UIImage(named: imageToLoad)
    print("ok")
    }
 }
}

I'm too newbie with swift! Can you help me here?

question from:https://stackoverflow.com/questions/66065276/swift-5-pushviewcontroller-is-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to show the DetailViewController you should change your code to

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    guard let vc = storyboard.instantiateViewController(withIdentifier: "Detail") as? DetailViewController else { return }
    
    vc.selectedImage = "images/logo_2-mini.png"
    print("Click (indexPath)   " + vc.selectedImage!) // I see it!
    
    self.present(vc, animated: true)
  }

This way you don't need to embed your ViewController into a navigation controller.

Also, I've created the storyboard variable since I don't see its declaration in your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...