外部控制器:发送到实例的无法识别的选择器

时间:2014-06-06 10:52:47

标签: ios objective-c

我已经看过类似问题一千次了,我通常可以回答这些问题......但是这次我遇到了同样的错误。

我有一个实现以下代码的UIViewController:

·H

#import <UIKit/UIKit.h>

@interface MMRHomeViewController : UIViewController

@end

.M

#import "MMRHomeViewController.h"
#import "MMRPageTwoViewController.h"
#import "MMRMenuViewController.h"

@interface MMRHomeViewController ()

@end

@implementation MMRHomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"HOME";
    UIImage *menuImage = [UIImage imageNamed:@"menu.png"];
    UIBarButtonItem *menuBtn = [[UIBarButtonItem alloc] initWithImage:menuImage style:UIBarButtonItemStylePlain target:(MMRMenuViewController *)self action:@selector(showMenu)];
    self.navigationItem.leftBarButtonItem = menuBtn;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Go to next page Action
- (IBAction)goToPage2:(id)sender {
    UIViewController *pageTwoCtrl = [[MMRPageTwoViewController alloc] init];
    [self.navigationController pushViewController:pageTwoCtrl animated:YES];
}

@end

相当直截了当。我试图在MMRMenuViewController视图控制器上调用showMenu()方法。

以下是MMRMenuViewController.m中的代码:

- (void)showMenu
{
    NSLog(@"Show menu");
}

再一次,死得很简单。

我已经检查了#import&#34; MMRMenuViewController.h&#34;我的MMRMenuViewController.h实现&#34; - (void)showMenu;&#34;

这是MMRMenuViewController代码:

.H     #import

@interface MMRMenuViewController : UIViewController

- (void)showMenu;

@end

.M

#import "MMRMenuViewController.h"

@interface MMRMenuViewController ()

@property (strong, nonatomic) IBOutlet UIImageView *avatarImageView;

@end

@implementation MMRMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.avatarImageView.layer.cornerRadius = 38.0;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did Load");
    NSLog(@"Self = %@", self);
    NSLog(@"Self avatarImageView = %@", self.avatarImageView);
    NSLog(@"Self View = %@", self.view);

    // Do any additional setup after loading the view from its nib.
    self.avatarImageView.layer.cornerRadius = 38.0;
    self.view.backgroundColor = [UIColor redColor];
    CGRect menuViewFrame = self.view.frame;
    menuViewFrame.origin.x = - [[UIScreen mainScreen] bounds].size.width;
    self.view.frame = menuViewFrame;
}

- (void)showMenu
 {
     NSLog(@"Show menu");
     //[self.navigationController.view insertSubview:self.view aboveSubview:self.view.superview];
}

@end

问题是我一直收到以下错误:

2014-06-06 13:28:24.705 BHSideBar [23137:60b] - [MMRHomeViewController showMenu]:无法识别的选择器发送到实例0x109416f00 2014-06-06 13:28:28.516 BHSideBar [23137:60b] *由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,原因:&#39; - [MMRHomeViewController showMenu]:无法识别的选择器已发送例如0x109416f00&#39; * 第一次抛出调用堆栈: (     0 CoreFoundation 0x000000010194a495 exceptionPreprocess + 165     1 libobjc.A.dylib 0x00000001016a999e objc_exception_throw + 43     2 CoreFoundation 0x00000001019db65d - [NSObject(NSObject)doesNotRecognizeSelector:] + 205     3 CoreFoundation 0x000000010193bd8d ___ forwarding _ + 973     4 CoreFoundation 0x000000010193b938 _CF_forwarding_prep_0 + 120     5 UIKit 0x0000000100257f06 - [UIApplication sendAction:to:from:forEvent:] + 80     6 UIKit 0x0000000100257f06 - [UIApplication sendAction:to:from:forEvent:] + 80     7 UIKit 0x0000000100257eb4 - [UIApplication sendAction:toTarget:fromSender:forEvent:] + 17     8 UIKit 0x0000000100334880 - [UIControl _sendActionsForEvents:withEvent:] + 203     9 UIKit 0x0000000100333dc0 - [UIControl touchesEnded:withEvent:] + 530     10 UIKit 0x000000010028ed05 - [UIWindow _sendTouchesForEvent:] + 701     11 UIKit 0x000000010028f6e4 - [UIWindow sendEvent:] + 925     12 UIKit 0x000000010026729a - [UIApplication sendEvent:] + 211     13 UIKit 0x0000000100254aed _UIApplicationHandleEventQueue + 9579     14 CoreFoundation 0x00000001018d9d21 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17     15 CoreFoundation 0x00000001018d95f2 __CFRunLoopDoSources0 + 242     16 CoreFoundation 0x00000001018f546f __CFRunLoopRun + 767     17 CoreFoundation 0x00000001018f4d83 CFRunLoopRunSpecific + 467     18 GraphicsServices 0x0000000103ac1f04 GSEventRunModal + 161     19 UIKit 0x0000000100256e33 UIApplicationMain + 1010     20 BHSideBar 0x00000001000022e3 main + 115     21 libdyld.dylib 0x0000000101fe25fd start + 1 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止 (lldb)

我不能为我的生活弄清楚我哪里出错了。这可能很简单,但我还没能解决它,这让我发疯了。任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:3)

&#34; showMenu&#34;和&#34; showMenu:&#34;是不一样的方法。

一个是没有参数的方法,另一个是带有一个参数的方法。

答案 1 :(得分:3)

您正在调用showMenu:(具有参数)而不是showMenu

编辑:您在showMenu对象上调用MMRHomeViewController,该对象没有声明showMenu的方法。在MMRMenuViewController上调用它肯定会有效。

答案 2 :(得分:1)

现在看起来在错误的对象上调用了-showMenu。检查对象类型以及该对象是否实现-showMenu

答案 3 :(得分:1)

信息不足。从控制台显示完整的错误消息。映射出发送视图控制器,目标视图控制器,如何创建它们以及它们如何相互指向。您应该为两个视图控制器提供标题。

发布调用showMenu的代码。

然后,当您准备好在MMRMenuViewController中调用showMenu时,设置断点并记录您尝试发送showMenu消息的对象的类。

最可能的两个原因是您认为目标对象不是MMRMenuViewController,或者方法签名不同,例如:你试图发送一个参数,或者大小写是不同的。

答案 4 :(得分:-1)

嗯......经过一段时间的搜索后,我无法通过目标属性访问另一个控制器。所以,我必须做的是将动作传递给navigationController,因为当前的Controller可以访问它。

感谢所有人的耐心和有用的答案。