Reputation: 81
I am implementing my application with story boards. My app can be opened by custom url Here is the code
- (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;
}
When this written it opens the view controller 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
the corresponding .m file code is
[delegate sendDataToHomePage:symbol.data];
[reader dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popToRootViewControllerAnimated:YES];
the function is present in ADMSViewController.m
-(void)sendDataToHomePage:(NSString *)vin
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"VIN" message:vin delegate:nil cancelButtonTitle:@"Oke" otherButtonTitles:nil];
[alert show];
}
Please help me solve this issue.
Here is the error i am getting
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)
Upvotes: 0
Views: 5681
Reputation: 12093
In the method
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
in your ADMSAppDelegate
you are setting controller.delegate = self
so when sendDataToHomePage
method is called it is looking in the ADMSAppDelegate
and not in ADMSViewController
as you think. self
here in your code is ADMSAppDelegate
.
And as you have stated
the function is present in ADMSViewController.m
This method (Function as you have referred to it as) needs to be implemented in ADMSAppDelegate
otherwise it will always return this error.
Upvotes: 0
Reputation: 4029
Your delegate
property refers to an AppDelegate
instance. And you are calling [delegate sendDataToHomePage]
. And that method is not defined in AppDelegate
, it's in ADMSViewController
. So it's obvious that it will crash with that message.
Upvotes: 0
Reputation: 1576
Since your delegate is defined as id
, this line of code is probably causing the issue:
[delegate sendDataToHomePage:symbol.data];
You need to update delegate
declaration with your protocol:
@property (nonatomic,assign) id <senddataProtocol> delegate;
You also need to claim in the header file of your App Delegate that it implements required methods of the protocol. Your AppDelegate.h should look something like this:
@interface AppDelegate : UIResponder <UIApplicationDelegate, senddataProtocol>
And finally, implement the method in your AppDelegate.m:
-(void)sendDataToHomePage:(NSString *)vin {
blah blah...
}
This should solve the issue.
Upvotes: 1
Reputation: 1
You are making appDelegate as the delegate of ADMSBarcodeScanner and implementing delegate method in ADMSViewController.m. May be it is crashing because it is not getting the method implementation there. Just check for respondToSelector method before calling any protocol method.
Upvotes: 0
Reputation: 2451
declare your delegate using the protocol you have created
@property(nonatomic,assign)id <senddataProtocol> delegate
Upvotes: 0