3

我正在开发我的第一个真正的 iPhone 应用程序,一个简单的待办事项列表应用程序来帮助我组织东西,除了我得到一个“发送到实例 0x 的无法识别的选择器”。

具体来说:

2010-02-20 14:30:09.200 ToDoApp[88562:20b] *** -[NSCFDictionary switchViews:]: unrecognized selector sent to instance 0x3d22de0

2010-02-20 14:30:09.201 ToDoApp[88562:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFDictionary switchViews:]: unrecognized selector sent to instance 0x3d22de0'

我环顾四周,发现这可能是 IB 中的连接问题,但我对整个连接的东西还是新手(伙计,我希望他们支持 Java 或 Python),所以这是它的布局方式。我有 3 个类,一个 SwitchViewController、一个 MainScreenViewController 和一个 ToDoListViewController。当我点击 MainScreenViewController 上的按钮时,我触发了引发此问题的“switchViews”功能。他们设置它的方式是按钮(UIBarButtonItem)具有“sentAction”转到switchViews。这个 ViewButton 有它的参考出口,作为 SwitchViewController 中的一个 IBOutlet。

所以这里是 SVC 的 .h:

#import <UIKit/UIKit.h>

@class MainScreenViewController;
@class ToDoListViewController;
@class EditViewController;

#define kMinimumGestureLength 25
#define kMaximumVariance 5

@interface SwitchViewController : UIViewController {
 MainScreenViewController *mainScreenViewController;
 ToDoListViewController *toDoListViewController;
 EditViewController *editViewController;
 IBOutlet UIBarButtonItem *viewButton;
 CGPoint gestureStartPoint;
}

@property (retain, nonatomic) MainScreenViewController *mainScreenViewController;
@property (retain, nonatomic) ToDoListViewController *toDoListViewController;
@property (retain, nonatomic) EditViewController *editViewController;
@property (retain, nonatomic) IBOutlet UIBarButtonItem *viewButton;
@property CGPoint gestureStartPoint;
-(IBAction)switchViews:(id)sender;  

对于 switchViews 功能:

-(IBAction) switchViews:(id)sender
{
 NSLog(@"Switching views");
 if(self.toDoListViewController.view.superview == nil){
  if(self.toDoListViewController ==nil){
   ToDoListViewController *toDoVC = [[ToDoListViewController alloc]     initWithNibName:@"ToDoListView" bundle:nil];
   self.toDoListViewController = toDoVC;
   //[toDoVC release];
  }
  [mainScreenViewController.view removeFromSuperview];
  [self.view insertSubview:toDoListViewController.view atIndex:0];
 }
 else{
  if(self.mainScreenViewController == nil){
   MainScreenViewController *mainController =     [[MainScreenViewController alloc] initWithNibName:@"MainScreenView" bundle:nil];
   self.mainScreenViewController = mainController;
   //[mainController release];
  }
  [toDoListViewController.view removeFromSuperview];
  [self.view insertSubview:mainScreenViewController.view atIndex:0];
 }
}

所以简而言之,我完全迷失了,这真的很令人沮丧。任何人有任何建议,或需要更多的代码?

4

7 回答 7

12

我们刚刚遇到了同样的问题。似乎我们在 AppDelegate 中释放了 ViewController 对象,然后我们的 nib 视图尝试调用 IBAction(在视图控制器上)。一半的时间我们得到“EXC_BAD_ACCESS”(又名消息释放对象),一半的时间我们得到“无法识别的选择器发送到实例”的NSCFString,NSCFArray,各种各样的东西(又名消息现在占用的内存区域由不同的对象)。

只需检查您的 ViewController 是否已发布。

于 2010-12-15T23:23:59.897 回答
4

好的,得到了​​解决方案指出给我。应该让它通过 FirstResponder 路由(我......真的不明白为什么会这样,但在这一点上我很高兴它可以工作。)

我不确定急救人员是如何工作的(我没有真正提到过它),但它......有效吗?如果有人想给我一个纲要,那会很有用……但是这个问题已经得到了回答。

于 2010-02-21T19:16:20.063 回答
0

我猜问题出在你的 nib 文件中。

该错误意味着单击按钮后,该按钮会尝试向switchViewNSDictionary 对象发送消息/方法调用,该对象当然没有此类方法。然后错误出现在按钮操作指向的位置。

检查此视图的笔尖。查看文件所有者并检查分配给它的类。SwitchViewController由于某种原因,请确保它不是字典。如果 File Owner 属性设置为字典类,它将加载字典并尝试将操作方法​​发送给它。

于 2010-02-20T21:45:57.837 回答
0

这也可能有帮助。Analyzer 例程建议我释放一些对象,我做到了。事实证明,我需要应用程序中的那些对象。在方法/消息/函数/例程/事物中的xAppDelegate.m文件(或其他文件)中,使用appDidFinishLaunching

UINavigationController *navController = [[UINavigationController alloc] init];

代替

UINavigationController *navController = [[[UINavigationController alloc] init] autorelease];

此外,Analyzer 建议我释放我推送到导航控制器上的对象。大错。该文件是我的菜单屏幕,当我按下按钮时,我收到一个unrecognized selector sent to instance. 显然,它正在调用IBActionand NSStringNSDictionary这并不好。

于 2012-01-06T20:03:33.500 回答
0

正确答案是这样的:

我们在应用程序委托中分配为第一个屏幕的视图控制器不应该在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中释放。在您的情况下,第一个屏幕是MainScreenViewController.

它(MainScreenViewController实例)应该在应用委托的dealloc方法中释放。

- (void)dealloc
{
    [_window release];
    [mainScreenViewController release];
    [super dealloc];
}
于 2012-01-08T07:55:03.623 回答
0

仅供参考,我在使用 ARC 时得到了这个,并且 xib 正在加载并放在屏幕上,但不知何故 VC 本身没有被保留。

我通过添加一个变量来在呈现它的 VC 中存储引用来解决它。

于 2013-02-05T04:27:12.417 回答
0

问题是,您已将UIViewController实例作为方法变量启动。因此视图控制器在方法执行后没有范围,因此它从内存循环中释放。因此,您必须将视图控制器实例设为类级别。

  @interface SwitchViewController () {
         ToDoListViewController *toDoVC;
         MainScreenViewController *mainController;
    }



-(IBAction) switchViews:(id)sender
{
     if (!toDoVC)
          toDoVC = [[ToDoListViewController alloc]     initWithNibName:@"ToDoListView" bundle:nil];
     if (!mainController)
          mainController =     [[MainScreenViewController alloc] initWithNibName:@"MainScreenView" bundle:nil];

 //Your stuff with the view controllers...
}
于 2014-11-25T06:08:30.027 回答