为什么在使用 navigationController.pushViewController() 时,我的根视图控制器会与预期视图一起推送?

Why is my root view controller getting pushed along with intended view when using navigationController.pushViewController()?

提问人:ilmfeemster 提问时间:10/23/2023 更新时间:10/23/2023 访问量:33

问:

我有一个位于导航控制器内部的 UIViewController。当我将另一个视图控制器推送到导航堆栈时,它被推送到根视图控制器的副本之上。副本可以在后台看到,并且可以与之交互和点击。视图是以编程方式制作的,没有情节提要或 xibs。

我尝试使用 UIButton 和导航项按钮进行测试,但发生了同样的事情。我无法找出为什么它发送两个视图控制器。

这是我的主视图控制器

import UIKit

class ViewController: UIViewController {
    let tableView = UITableView()
    let countries = ["US", "UK", ...] //an array of strings
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .blue
        setupUI()
        
        tableView.delegate = self
        tableView.dataSource = self
        
    }
    
    private func setupUI() {
        view.addSubview(tableView)
        self.tableView.register(FlagCell.self, forCellReuseIdentifier: FlagCell.identifier)
        tableView.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    }
    
}

extension ViewController : UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return countries.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: FlagCell.identifier, for: indexPath) as! FlagCell
        let country = countries[indexPath.row]
        let image = UIImage(named: country)
        cell.flagImageView.image = image
        cell.countryNameLabel.text = country
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = FlagViewController()
        let image = UIImage(named: countries[indexPath.row])
        vc.flagView.image = image
        navigationController?.pushViewController(vc, animated: true)
    }
}

这是我的细节视图控制器

import UIKit

class FlagViewController: ViewController {

    let flagView: UIImageView = {
        let flagView = UIImageView()
        return flagView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    
    // MARK: - UI Setup
    private func setupUI() {
        self.view.addSubview(flagView)
        flagView.translatesAutoresizingMaskIntoConstraints = false
        
        
        NSLayoutConstraint.activate([
            flagView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100),
            flagView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -100),
            flagView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            flagView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            
            flagView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            flagView.centerYAnchor.constraint(equalTo: self.view.centerYAnchor),
        ])
    }
}
Swift UIViageController UIKitationController UIKuageController

评论

0赞 HangarRash 10/23/2023
虽然可能不相关,但您的类是否使用您自己添加的表视图进行扩展而不是扩展?ViewControllerUIViewControllerUITableViewController
1赞 Sweeper 10/23/2023
你写了,但不应该吗?class FlagViewController: ViewControllerclass FlagViewController: UIViewController
0赞 ilmfeemster 10/23/2023
@Sweeper 这就是问题所在,详细视图控制器符合“ViewController”(我的主视图控制器的类名)。更改为“UIViewController”后,它按预期工作。
0赞 ilmfeemster 10/23/2023
@HangarRash我扩展了它,因为我的印象可能是更好的实践或更具可读性。

答:

0赞 ilmfeemster 10/23/2023 #1

通过替换

class FlagViewController: ViewController {...}

使用下面的代码,阻止我的详细信息视图控制器符合我的主视图控制器类。

class FlagViewController: UIViewController {...}