【问题标题】:Cannot open TableViewController无法打开 TableViewController
【发布时间】:2020-10-03 22:26:06
【问题描述】:

在处理一些包含登录功能的 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"

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

谢谢

【问题讨论】:

  • 您的目标选择器被定义为具有一个参数(发送者)。你的实现没有。
  • @Phillip Mills 它有参数,刚刚编辑和添加。

标签: ios swift5


【解决方案1】:

异常原因清楚地表明 didTapButton(sender...翻译到 Objective-C 为 didTapButtonWithSender:

IBAction 中,您必须插入下划线字符

@IBAction func didTapButton(_ sender: Any) 

选择器可以很简单

#selector(didTapButton)

【讨论】:

  • 这个方法包含这个参数,我更新了上面的。
  • IBAction 必须包含参数并且下划线字符仅隐藏外部参数名称。
  • 它有参数和下划线,我仍然得到错误
  • 在 Interface Builder 中重新连接操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 2019-01-06
  • 2014-05-10
  • 2021-12-01
  • 1970-01-01
相关资源
最近更新 更多