[英]Foreign controller : Unrecognised selector sent to instance
我見過類似的問題,問過一千遍,我通常可以回答這些問題……但是這次,我是一個被同樣錯誤困擾的人。
我有一個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“ MMRMenuViewController.h”,我的MMRMenuViewController.h實現了“-(void)showMenu;”。
這是MMRMenuViewController代碼:
.H#導入
@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] *由於未捕獲而終止應用程序異常'NSInvalidArgumentException',原因:'-[MMRHomeViewController showMenu]:無法識別的選擇器發送到實例0x109416f00'*首次拋出調用堆棧:(0 CoreFoundation 0x000000010194a495 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001016a999e objc_exception_throwx + 43 2 CoreFoundation (NSObject)didNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010193bd8d ___forwarding_ + 973 4 CoreFoundation 0x000000010193b938 _CF_forwarding_prep_0 + 120 5 UIKit 0x0000000100257f06-[UIApplication sendAction:to:from:for:Event:forEvent:] + 80 6 UIKit 0x06 :] + 80 7 UIKit 0x0000000100257eb4-[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17 8 UIKit 0x0000000100334880-[UIControl _send ActionsForEvents:withEvent:] + 203 9 UIKit 0x0000000100333dc0-[UIControl touchesEnded:withEvent:] + 530 10 UIKit 0x000000010028ed05-[UIWindow _sendTouchesForEvent:] + 701 11 UIKit 0x000000010028f6e4-[UIWindow sendEvent:] + 925 12 UIKit 0x000000010 ] + 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主+ 115 21 libdyld.dylib 0x0000000101fe25fd start +1)libc ++ abi.dylib:以NSException類型的未捕獲異常終止(lldb)
而且我無法為自己的生活弄清楚哪里出了問題。 它可能很簡單,但是我無法解決它,這使我發瘋。 任何幫助將不勝感激。
“ showMenu”和“ showMenu:”是不同的方法。
一種是沒有參數的方法,另一種是帶有一個參數的方法。
您正在調用showMenu:
具有參數),而不是showMenu
。
編輯:你調用showMenu
一個上MMRHomeViewController
其中有對象沒有方法中聲明showMenu
。 在MMRMenuViewController
上調用它,它肯定會工作。
現在看來, -showMenu
在錯誤的對象上被調用。 檢查對象類型以及該對象是否實現-showMenu
。
信息不足。 從控制台顯示完整的錯誤消息。 繪制出發送視圖控制器,目標視圖控制器,它們的創建方式以及它們如何獲得彼此的指針。 您可能應該為兩個視圖控制器都提供標題。
發布調用showMenu的代碼。
然后,當您准備在MMRMenuViewController中調用showMenu時,請設置一個斷點並記錄您要發送showMenu消息的對象的類。
兩種最可能的原因是您認為目標對象不是MMRMenuViewController,或者方法簽名不同(例如,您嘗試發送參數)或大小寫不同。
好吧……經過一會兒搜索之后,我無法通過target屬性訪問另一個控制器。 所以,我要做的就是將動作傳遞給navigationController,因為當前Controller可以訪問該動作。
感謝您的耐心配合和有用的回答。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.