首页 > 解决方案 > 无法打开 TableViewController

问题描述

在处理一些包含登录功能的 IOS 项目 (Swift5) 时,我遇到了一个奇怪的问题,即在按钮上单击一个新的 UITableViewController 打开,其中包含来自数据库的一些记录。奇怪,因为相同的功能在登录视图中没有任何问题,并且在用户登录后无法从视图中运行。

这是打开新控制器的方法:

 @IBAction func didTapButton(sender: Any) {
    
   // let story : UIStoryboard = UIStoryboard(name: "Main",        
   bundle:nil)
    
    let categories = CategoriesTableViewController()
    navigationController?.pushViewController(categories, 
    animated: true)
    

    //        let controller =  
    story.instantiateViewController(identifier: 
    "CategoriesTableViewController") as! 
    CategoriesTableViewController
    //
    //        present(controller, animated: true)
    
    }

这是我登录到此应用程序后单击运行上述方法时发生的错误:

Exception   NSException *   "-  
[project7_4_adm.ViewController       
didTapButtonWithSender:]: unrecognized selector sent 
to instance 0x7f9889222260" 0x0000600001d35860

我在 google 中找到了一些解决方案,即向我所做的按钮添加目标:

   btn.addTarget(self, action: 
   #selector(didTapButton(sender:)), for: 
   .touchUpInside)

但即使我这样做了,我也得到了一个错误:

    Thread 1: "-
    [project7_4_adm.CategoriesTableViewController  
    initWithIdentifier:source:destination:]: unrecognized 
    selector sent to instance 0x7fac3a11df90"

我可以请你写信给我这个问题的原因是什么?

谢谢

标签: iosswift5

解决方案


异常原因清楚地表明将didTapButton(sender...转换为 Objective-C 为didTapButtonWithSender:

IBAction你必须插入一个下划线字符

@IBAction func didTapButton(_ sender: Any) 

选择器可以很简单

#selector(didTapButton)

推荐阅读