The changeView_ShowContact method will make a view controller to show up just fine. But when I call the changeView_ShowContact from another class it wont work. Im getting an error in the log "unrecognized selector sent to instance."
#protocol callingActions_fromMainNavigation <NSObject>
- (IBAction)changeView_ShowContact:(id)sender;
#end
**#interface Navigation_Main : NSViewController**
#property (nonatomic, strong) id <callingActions_fromMainNavigation> delegate;
**#implementation Navigation_Main**
#synthesize delegate;
- (IBAction)changeView_ShowContact:(id)sender;
{
NSLog(#"********************ShowContact");
AddStuffViewController = [[pageContact alloc] initWithNibName:#"pageContact" bundle:nil];
[AddStuffView addSubview:[AddStuffViewController view]]; //<This call here works ok.
}
.
#interface contacts : NSObject <callingActions_fromMainNavigation>
**#implementation contacts**
-(void)myMethodCall:(id)sender;
{
Navigation_Main *NavMain = [[Navigation_Main alloc] initWithNibName:#"Navigation_Main.h" bundle:nil];
[NavMain setDelegate:self];
[self changeView_ShowContact:nil];
//I need to call the (IBAction)changeView_ShowContact in the Main Navigation. This
//code is not working.
}
It looks like you are having your contacts adopt the callingActions_fromMainNavigation protocol with out implementing changeView_ShowContact: in the contacts class.
Related
So I am calling delegate function but some how its not getting called, I tried everything from all the other similar threads but nothing works. It looks all good but the method is still not called. here is my code below -
So i created protocol like this -
AuthViewController.h
#class AuthViewController;
#protocol AuthViewControllerDelegate <NSObject>
- (void)updateNavigation:(NSString*)pageType
message:(NSString*)message;
#end
created a property -
#property (nonatomic, weak)id delegate;
And called the function in AuthViewController.m -
[self.delegate updateNavigation:#"xx" message:#"xx"];
Then in other class -
AssociateViewController.h
#interface AssociateViewController : UIViewController <AuthViewControllerDelegate>
#property (nonatomic, strong)AuthViewController *vc;
#End
AssociateViewController.m
First set the delegate in a button action or viewWillAppear(I tried both)-
self.vc = [[AuthViewController alloc] init];
self.vc.delegate = self;
And the here is the method which is somehow never called :( -
- (void)updateNavigation:(NSString*)pageType
message:(NSString*)message;
{
//method to do
}
Any help would be much appreciated. Thanks!
Im trying to implement delegate design pattern.
I have two view controllers as followsm
CallViewViewController
CEPeoplePickerNavigationController
This is my interface definition of CallViewViewController
#interface CallViewViewController ()<CEPeoplePickerNavigationControllerDelegate>{
}
#property(nonatomic)CustomMessageClass * customMessageClassObject;
#end
In my implementation, i have of-course implemented the delegate method
-(void)cePeoplePickerNavigationController:(CEPeoplePickerNavigationController *)peoplePicker didFinishPickingPeople:(NSArray *)peopleArg values:(NSDictionary *)valuesArg
{
NSLog(#"can populate the tableview");
}
This is the interface definition of my second class,
#protocol CEPeoplePickerNavigationControllerDelegate;
#interface CEPeoplePickerNavigationController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
id<CEPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
ABAddressBookRef addressBook;
}
#property (nonatomic, assign) id<CEPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
#property (nonatomic, retain) CEPeoplePickerNavigationController *ppnc;
#end
#protocol CEPeoplePickerNavigationControllerDelegate <NSObject>
- (void)cePeoplePickerNavigationController:(CEPeoplePickerNavigationController *)peoplePicker didFinishPickingPeople:(NSArray*)people values:(NSDictionary *)values;
#end
when the user presses submit button,Im executing the following code,
if ([self.ppnc.peoplePickerDelegate respondsToSelector:#selector(cePeoplePickerNavigationController:didFinishPickingPeople:values:)])
[self.ppnc.peoplePickerDelegate cePeoplePickerNavigationController:self.ppnc didFinishPickingPeople:sortedPeople values:[NSDictionary dictionaryWithDictionary:self.selectedValues]];
But the data is not being passed back to the previous view controller. Why so?
UPDATE
i tried the following code to move form first to second view controller,
CEPeoplePickerNavigationController *nextVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PeoplePickerNavigationController"];
nextVC.peoplePickerDelegate = self;
[self presentViewController:nextVC animated:YES completion:nil];
but it throws the following error,
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setPeoplePickerDelegate:]: unrecognized selector sent to instance 0x101054800'
You just need to pass data using Custom Delegate
Write Below code on your CEPeoplePickerNavigationController's .h file
#class CEPeoplePickerNavigationController;
#protocol CEPeoplePickerNavigationController <NSObject>
- (void)previousViewController:(CEPeoplePickerNavigationController *)controller itemToSend:(NSString *)item;
#end
#interface CEPeoplePickerNavigationController : UIViewController
#property (nonatomic, weak) id < CEPeoplePickerNavigationController Delegate> delegate;
#end
on your moving to previous view controller event you need to put below code
[self.delegate previousViewController:self itemToSend:#"From Previous VC"];
[self.navigationController popViewControllerAnimated:true];
On your CallViewViewController you just need to add below code on .m file
#import "CEPeoplePickerNavigationController.h"
#interface ViewController ()< CEPeoplePickerNavigationController Delegate>
Add method declare on previous view controller
- (void)previousViewController:(CEPeoplePickerNavigationController *)controller itemToSend:(NSString *)item
{
NSLog(#"from CEPeoplePickerNavigationController %#",item);
}
make sure when you navigate to CEPeoplePickerNavigationController you need to assign delegate to self as below
CEPeoplePickerNavigationController *objVC = [self.storyboard instantiateViewControllerWithIdentifier:#"CEPeoplePickerNavigationController"];
objVC.delegate = self;
[self.navigationController pushViewController:infoController animated:YES];
When you are moving from CallViewViewController to CEPeoplePickerNavigationController , there must be some sort of navigation where you are pushing or applying segue to go the next VC.
Just add this line of code there -
If you are using segue --
CEPeoplePickerNavigationController *nextVC = [segue destinationViewController];
nextVC.peoplePickerDelegate = self;
If you are using instantiate --
CEPeoplePickerNavigationController *nextVC = [self.storyboard instantiateWithStoryboardid:#"YOUR STORYBOARD ID"];
nextVC.peoplePickerDelegate = self;
Once this is done, it will respond to the delegate from the next controller
EDIT
Please clear your CEPeoplePickerNavigationController .h code to the following
#protocol CEPeoplePickerNavigationControllerDelegate <NSObject>
- (void)cePeoplePickerNavigationController:(CEPeoplePickerNavigationController *)peoplePicker didFinishPickingPeople:(NSArray*)people values:(NSDictionary *)values;
#end
#interface CEPeoplePickerNavigationController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
ABAddressBookRef addressBook;
}
#property (nonatomic, assign) id<CEPeoplePickerNavigationControllerDelegate> peoplePickerDelegate;
#property (nonatomic, retain) CEPeoplePickerNavigationController *ppnc;
#end
I can't make delegate work from static library that i build. If I do in separate project it works correctly. Can you please check my code:
StaticSdk.h
#protocol SdkDelegate;
#interface Sdk : NSObject
#property (nonatomic, weak) id <SdkDelegate> delegate;
+(void)startAction
-(void)taskComplete;
#end
#protocol SdkDelegate <NSObject>
-(void)taskGetDataCompleted:(NSMutableData*)pushedData;
#end
StaticSdk.m
+(void)startAction{
SdkDelegate *sdk = [[SdkDelegate alloc] init];
[sdk.delegate taskGetDataCompleted:dataMy];
}
Than I am including this static library to my project and delegate which i specified is not executed.
Project code
ViewController.h
#interface ViewController : UIViewController <SdkDelegate>
-(IBAction) Connect: (id) sender;
#end
ViewController.m
- (void)taskGetDataCompleted:(NSMutableData*)pushedData
{
[imageView setImage:[[UIImage alloc] initWithData:pushedData]];
}
I'm guessing you're failing to compile on:
SdkDelegate *sdk = [[SdkDelegate alloc] init];
The issue here is that you cannot instantiate a protocol. Instead you have a class adopt that protocol, which it looks like you did correctly with ViewController. What you need to do now is assign your instance of ViewController to the delegate property of your Sdk instance, sometime before you call startAction.
I have a view controller on the StoryBoard with my custom class. Here the code:
BGMDatePickerViewController.h
#import <UIKit/UIKit.h>
//declare a protocol with name
#protocol DatePickerViewControllerDelegate //declare a delegate so that this class can notify another class when a user selects a chart
//protocol methods
//- (void)userDataChangedWithUsername;
#end
//declare a class with inheritance
#interface BGMDatePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate>
//create a public property
#property (weak, nonatomic) id<DatePickerViewControllerDelegate> delegate; //property to store the delegate
#end
I would like to show this ViewController on another one with delegate. Here the code:
BGMChartViewController.h
#import <UIKit/UIKit.h>
#import "BGMDatePickerViewController.h"
//declare a class with inheritance
#interface BGMChartViewController : UIViewController <UIActionSheetDelegate, DatePickerViewControllerDelegate> //conforms UIActionSheetDelegate and my custom DatePickerViewControllerDelegate protocols
//create a public IB methods
- (IBAction)showDateEntryForm:(id)sender;
#end
BGMChartViewController.m
#import "BGMChartViewController.h"
#interface BGMChartViewController ()
//create a private properties
#property (strong, nonatomic) UIPopoverController *datePickerPopover;
#end
#implementation BGMChartViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)showDateEntryForm:(id)sender
{
DDLogVerbose(#"%s: has been started...", __FUNCTION__);
BGMDatePickerViewController *datePickerVC = [self.storyboard instantiateViewControllerWithIdentifier:#"dateEntryFormVC"];
datePickerVC.delegate = self; //HERE THE FALL!!!!!
self.datePickerPopover = [[UIPopoverController alloc] initWithContentViewController:datePickerVC];
//define the popover size
self.datePickerPopover.popoverContentSize = CGSizeMake(320.0, 400.0);
//let’s display it
[self.datePickerPopover presentPopoverFromRect:[(UIButton *)sender frame]
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
}
#end
On the line which I marked it just fall with:
-[BGMDatePickerViewController setDelegate:]: unrecognized selector sent to instance 0x78a3bcf0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BGMDatePickerViewController setDelegate:]: unrecognized selector sent to instance 0x78a3bcf0'
If I comment this problem line it's ok I see my custom view controller so that means that I am ok with instantiateViewControllerWithIdentifier tag...
I don't understand what do I do wrong?
Modify to the below line and check:-
#protocol DatePickerViewControllerDelegate<NSObject>
'-[MTviewFilesVC launchVF]: unrecognized selector sent to instance 0x1e59fcd0'
I added a method to a class but calling it creates 'unrecognized selector' run time error
The calling code is:
self.viewFilesVCPtr = [[MTviewFilesVC alloc] init];
[self.viewFilesVCPtr launchVF];
This works if, for example, I substitute viewDidLoad which exists already hence I
think the calling code is OK.Is there something else I need to add to the declaration of lanuchVF
to make it visible?
The method declaration, etc is:
.h:
#import "DirectoryWatcher.h"
#interface MTviewFilesVC : UITableViewController <QLPreviewControllerDataSource,
QLPreviewControllerDelegate,
DirectoryWatcherDelegate,
UIDocumentInteractionControllerDelegate>
-(IBAction)saveViewFiles;
- (void)launchVF;
#end
.m:
#interface MTviewFilesVC ()
#property (nonatomic, strong) DirectoryWatcher *docWatcher;
#property (nonatomic, strong) NSMutableArray *documentURLs;
#property (nonatomic, strong) UIDocumentInteractionController *viewFileController;
-(void) launchVF;
#end
...
- (void)lanuchVF
{
UIStoryboard *settingsStoryBoard = [UIStoryboard storyboardWithName:
#"viewFiles" bundle:nil];
UIViewController *initialViewFilesVC = [settingsStoryBoard instantiateInitialViewController];
initialViewFilesVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:initialViewFilesVC animated:YES];
}
Your method name in the .m has a typo, lanuchVF instead of launchVF :-)