iphone - Xcode:单击按钮会导致 NSInvalidArgumentException

标签 iphone objective-c xcode cocoa ibaction

我正在使用 Xcode 4.3.2。

我是 Xcode 新手。我正在构建一个应用程序,该应用程序必须在单击按钮时更改为不同的 View 。我的文件是:AppDelegate.h/.m、GreenViewController.h/.m、SwitchViewController.h/.m、GreenView.xib - 我没有使用 Storyboard,但我的项目要求我不使用它们(向后兼容性问题) )。

这是我的问题(看起来很简单):当单击 UIButton(放置在 GreenView.xib 中)时,我尝试打印到控制台。这是我的 GreenViewController.h 代码

#import <UIKit/UIKit.h>
@interface GreenViewController : UIViewController
- (IBAction)switchViews:(id)sender;
@end

这是我的 GreenViewController.m(已弃用)代码:

#import "GreenViewController.h"
@implementation GreenViewController

- (IBAction) switchViews:(id)sender {
    NSLog(@"Button Pressed!");
}

GreenView.xib 的所有者是 GreenViewController。

出于某种原因,仅当按下 UIButton(在 GreenView.xib 中)时才会出现错误:

2012-10-09 18:07:38.490 MyViewSwitcher[8655:f803] -[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660
2012-10-09 18:07:38.492 MyViewSwitcher[8655:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SwitchViewController switchViews:]: unrecognized selector sent to instance 0x688a660'

看来 SwitchViewController 期望从“switchViews”方法中得到一些东西,但“switchViews”仅在 GreenViewController 中列出。之前,我在 SwitchViewController 中有“switchViews”,但我删除了与该方法对应的所有代码以及所有连接。再次,我仔细检查了 GreenViewController 中的“switchViews”是否已连接到 GreenView.xib 中找到的 UIButton。我已经清理并重新构建了我的项目,但仍然收到此错误。

感谢您的帮助!

最佳答案

您的错误表明您正在 SwitchViewController 实例上调用 switchViews: 方法。由于 SwitchViewController 类没有 switchViews: 的定义(因为你删除了它),它不知道要做什么,然后崩溃了。

不想告诉你这一点,但你的按钮已连接到 SwitchViewControllerswitchViews: 方法。你说“我已经仔细检查过 GreenViewController 中的‘switchViews’是否连接到 GreenView.xib 中找到的 UIButton”。嗯,是的,从你的车祸来看,确实如此。但是您确定它连接到 GreenViewController 实例的 switchViews: 函数吗?您是如何查看这些信息的?

我建议删除连接检查器中 UIButton 的所有连接。然后将其重新连接到 View Controller (你说的是 GreenViewController)。然后它应该显示 IBActions 列表,该列表应该只是 switchViews: 方法。

如果你这样做了,但它仍然不起作用。尝试删除该按钮并重新制作,然后重新连接。

关于iphone - Xcode:单击按钮会导致 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810733/

相关文章:

objective-c - 静态实例使用 ARC 过早释放属性

ios - Google Analytics 3.12 在 iOS 上崩溃

iphone - Twitter搜索主题标签

objective-c - 抑制行特定的 XCode 编译器警告

ios - 在 UITextView 上设置语音焦点

iphone - 在新部分的表格 View 中插入一行

iphone - 从表和SQLite数据库中删除行

ios - 拖动包含 UIScrollView 的 UIView

ios - 如何正确覆盖 HashCode 方法?

ios - 在 ObservableObject 中导入变量