[英]Unrecognized Selector Sent to instance from App Delegate
我正在用故事板实现我的应用程序。 可以通过自定义网址打开我的应用程序,这是代码
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication: (NSString *)sourceApplication annotation:(id)annotation
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
ADMSBarcodeScanner *controller = (ADMSBarcodeScanner*)[mainStoryboard instantiateViewControllerWithIdentifier: @"barcode_scanner"];
controller.delegate =self;
[navigationController pushViewController:controller animated:YES];
return true;
}
撰写本文时,它将打开视图控制器ADMSBarcodeScanner.h
@protocol senddataProtocol <NSObject>
-(void)sendDataToHomePage:(NSString *)vin;
@end
@interface ADMSBarcodeScanner : UIViewController < ZBarReaderDelegate >
{
UIImageView *resultImage;
UITextView *resultText;
UIView *cameraView;
}
@property(nonatomic,assign)id delegate;
@property (nonatomic, retain) IBOutlet UIImageView *resultImage;
@property (nonatomic, retain) IBOutlet UITextView *resultText;
@property (retain, nonatomic) IBOutlet UITextField *resultField;
@property (weak, nonatomic) IBOutlet UIView *cameraView;
@end
相应的.m文件代码是
[delegate sendDataToHomePage:symbol.data];
[reader dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popToRootViewControllerAnimated:YES];
该函数存在于ADMSViewController.m中
-(void)sendDataToHomePage:(NSString *)vin
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"VIN" message:vin delegate:nil cancelButtonTitle:@"Oke" otherButtonTitles:nil];
[alert show];
}
请帮我解决这个问题。
这是我遇到的错误
2014-01-08 15:49:21.466 Autofunds[2476:907] -[ADMSAppDelegate sendDataToHomePage:]: unrecognized selector sent to instance 0x1cd30580
2014-01-08 15:49:21.478 Autofunds[2476:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ADMSAppDelegate sendDataToHomePage:]: unrecognized selector sent to instance 0x1cd30580'
*** First throw call stack:
(0x32c762a3 0x3a8f697f 0x32c79e07 0x32c78531 0x32bcff68 0xd1eb5 0xe51cb 0xeaaef 0xe2ff5 0x3358d0f5 0x32c4b683 0x32c4aee9 0x32c49cb7 0x32bbcebd 0x32bbcd49 0x3676f2eb 0x34ad2301 0xd14d5 0x3ad2db20)
libc++abi.dylib: terminate called throwing an exception
(lldb)
由于您的委托被定义为id
,因此以下代码行可能会导致问题:
[delegate sendDataToHomePage:symbol.data];
您需要使用协议更新delegate
声明:
@property (nonatomic,assign) id <senddataProtocol> delegate;
您还需要在App Delegate的头文件中声明它实现了协议的必需方法。 您的AppDelegate.h应该看起来像这样:
@interface AppDelegate : UIResponder <UIApplicationDelegate, senddataProtocol>
最后,在您的AppDelegate.m中实现该方法:
-(void)sendDataToHomePage:(NSString *)vin {
blah blah...
}
这应该可以解决问题。
使用您创建的协议声明您的代表
@property(nonatomic,assign)id <senddataProtocol> delegate
您将使appDelegate成为ADMSBarcodeScanner的委托,并在ADMSViewController.m中实现委托方法。 可能是因为没有在那里实现方法而崩溃。 只需在调用任何协议方法之前检查一下responseToSelector方法即可。
您的delegate
属性引用一个AppDelegate
实例。 您正在调用[delegate sendDataToHomePage]
。 而且该方法未在AppDelegate
定义,而是在ADMSViewController
中ADMSViewController
。 因此很明显它将随着该消息崩溃。
在方法中
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
在ADMSAppDelegate
您正在设置controller.delegate = self
因此,在sendDataToHomePage
方法时,它会在ADMSAppDelegate
查找,而不是您认为的那样在ADMSViewController
中ADMSViewController
。 self
在你的代码这里是ADMSAppDelegate
。
正如你所说的
该函数存在于ADMSViewController.m中
需要在ADMSAppDelegate
实现此方法(如您所称的功能),否则它将始终返回此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.