我正在使用 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:
的定义(因为你删除了它),它不知道要做什么,然后崩溃了。
不想告诉你这一点,但你的按钮已连接到 SwitchViewController
的 switchViews:
方法。你说“我已经仔细检查过 GreenViewController 中的‘switchViews’是否连接到 GreenView.xib 中找到的 UIButton”。嗯,是的,从你的车祸来看,确实如此。但是您确定它连接到 GreenViewController
实例的 switchViews:
函数吗?您是如何查看这些信息的?
我建议删除连接检查器中 UIButton 的所有连接。然后将其重新连接到 View Controller (你说的是 GreenViewController)。然后它应该显示 IBActions 列表,该列表应该只是 switchViews:
方法。
如果你这样做了,但它仍然不起作用。尝试删除该按钮并重新制作,然后重新连接。
关于iphone - Xcode:单击按钮会导致 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810733/