Delegate nullified when present viewController - ios

With these codes, I set the delegate for the ViewController that is about to be presented. Based the logged result, the delegate is not nil when is assigned to the viewController.
NewEventViewController *NewEventView = (NewEventViewController *)[[self storyboard] instantiateViewControllerWithIdentifier:#"AddView"];
NewEventView.delegate = self;
NSLog(#"delegate %#", NewEventView.delegate);
[self presentViewController:NewEventView animated:YES completion:nil];
Here is my declaration of protocol in NewEventViewController.
#protocol NewEventDelegate <NSObject>
-(void)addNewEventToProject:(Events *)event;
-(void)addProject:(Projects *)project;
#end
I declared a delegate like this
#property (nonatomic, assign) id <NewEventDelegate> delegate;
When the viewController is presented, the delegate seems to be nullified. I logged the delegate with NSLog(#"Delegate %#", self.delegate); the outcome is Delegate (null) What is the problem in my code.
My purpose is to call the delegate method when the viewcontroller is dismissed. The delegate method will pass back a Event object that will be added to my Realm database. Thank you very much!

Related

Custom Delegate Methods not being called

Look at the following code.
In CusFormViewController.h
#interface CusFormViewController : CusBaseViewController
#protocol CusFormViewControllerDelegate <NSObject>
-(void)ticketCreated:(NSString*)ticketID;
-(void)ticketFormRenderingFinished;
#end
#property (nonatomic, weak) id<CusFormViewControllerDelegate> delegate;
In CusFormViewController.m
if ([self.delegate respondsToSelector:#selector(ticketFormRenderingFinished)])
[self.delegate ticketFormRenderingFinished];
if ([self.delegate respondsToSelector:#selector(ticketCreated:)])
[self.delegate ticketCreated:ticket_id];
In ViewController.m
#import "CusFormViewController.h"
#interface ViewController ()<CusFormViewControllerDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
CusFormViewController *formVC = [[CusFormViewController alloc] init];
[formVC setDelegate:self];
}
-(void)ticketCreated:(NSString*)ticketID{
NSLog(#"ticket created.");
}
-(void)ticketFormRenderingFinished{
NSLog(#"ticket form rendered.");
}
The ticketCreated & ticketFormRenderingFinished are not being called.
Most common reason for delegate method not being called is dealing with incorrect objects.
Ensure that CusFormViewController object created from
ViewController is the same which you are presenting on screen and
that the delegate is being set on the same object. I truly believe
you are creating a new CusFormViewController object and setting
delegate on that.
In CusFormViewController, before calling delegate, check the
existence of delegate as well. This is a safety check, you can also put a
NSLog statement to double check if your delegate exists or not. You
are failing here.
If you are segueing from ViewController to CusFormViewController then you set delegate in prepareForSegue: and not in viewDidLoad.
As a side note, put a NSLog statement in viewDidLoad of your CusFormViewController and print self.delegate to check the property setting.
Your controller formVC is dealloced after the function viewDidLoad executed. Create strong reference on your formVC for example like this:
#interface ViewController ()<CusFormViewControllerDelegate>
{
CusFormViewController *_formVC;
}
- (void)viewDidLoad {
[super viewDidLoad];
_formVC = [[CusFormViewController alloc] init];
[formVC setDelegate:self];
}
Hey try to create instance of CusFormViewController using below method
CusFormViewController * _formVC=[[UIStoryboard storyboardWithName:storyboardName bundle: nil]instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_ CusFormViewController"];

Delegates method never being called

I am quite new to IOS, and I was working with delegates to call a method in parent ViewController.
ViewControllerRegistration.h
#protocol RegistrationViewControllerDelegate;
#interface ViewControllerRegistration : UIViewController
#property (nonatomic, weak) id<RegistrationViewControllerDelegate> delegate;
- (IBAction)registerNewUser:(id)sender;
#end
// 3. Definition of the delegate's interface
#protocol RegistrationViewControllerDelegate <NSObject>
-(void)loginResult:(NSString*)loginData;
#end
ViewControllerRegistration.m
- (void)registerNewUser:(id)sender {
id<RegistrationViewControllerDelegate> strongDelegate = self.delegate;
// Our delegate method is optional, so we should
// check that the delegate implements it
if ([strongDelegate respondsToSelector:#selector(loginResult:)]) {
[strongDelegate loginResult: #"#WHY YOU NOT BEING CALLED?"];
}
Parents:
ViewController.h
#import "ViewControllerRegistration.h"
#interface ViewController : UIViewController <GPPSignInDelegate,FBSDKLoginButtonDelegate,RegistrationViewControllerDelegate>
#end
ViewController.m
#implementation ViewController
#synthesize signInButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
ViewControllerRegistration *detailViewController = [[ViewControllerRegistration alloc] init];
// Assign self as the delegate for the child view controller
detailViewController.delegate = self;
}
//Local Strategy
- (IBAction)navigateToLocalSignUpNavBtn:(id)sender {
[self performSegueWithIdentifier:#"SegueRegisterUserAction" sender:self];
}
// Implement the delegate methods for RegistrationViewControllerDelegate
-(void)loginResult:(NSString*)loginData{
NSLog(#"I am called");
//^Above is never printed. That means delegate method is never called
}
Please note, my parent view controller is embedded in a navigation controller.
The delegate method is never called. Tried debugging, but in vain.
The view controller you are creating in viewDidLoad isn't the same one that you will be segueing to. It's another instance that will be deallocated at the end of the method. You need to access the correct instance in prepareForSegue and set the delegate there.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"SegueRegistrationUserAction"]) {
[(ViewControllerRegistration *)segue.destinationViewController setDelegate:self];
}
}
Yesterday, I had explained this in length on this thread. Please take a look and let me know if you have any specific question post that. I hope this would help you.

My program not going inside the delegate method

I am trying to use a delegate method in AddCity.m controller that I created in the City.m controller. The method is
City.m
- (void)EditCityController:(id)controller didEditItem:(id)item
{
if(item)
{
NSDictionary *d = (NSDictionary *)item;
[self.model addNewCity:[d valueForKey:#"cityName"] forProvince:self.ro inMayor:[d valueForKey:#"mayorName"] inPopulation:[[d valueForKey:#"population"]intValue] inYearEstablished:[[d valueForKey:#"yearEstablished"]intValue]];
[self.model saveChanges];
}
// Dismiss the modal view controller
[controller dismissViewControllerAnimated:YES completion:nil];
}
City.h
#interface Cities :
UITableViewController<NSFetchedResultsControllerDelegate, EditCityDelegate>
And then in AddCity.h i have
#protocol EditCityDelegate;
#interface AddCity : UIViewController<UITextFieldDelegate>
#property (nonatomic, assign) id <EditCityDelegate> delegate;
#protocol EditCityDelegate <NSObject>
- (void) EditCityController:(id)controller didEditItem:(id)item;
#end
I am calling it from AddCity.m (Currently trying to the cancel button).
- (IBAction)cancel:(id)sender {
[self.delegate EditCityController:self didEditItem:nil];
}
When i put a breakpoint what happens is that it hits the self.delegate line but it does not go into the EditCityController method in the City.m controller.Any idea on what i am doing wrong?
I assume you call AddCity.m in City.m and present it using presentViewController
AddCity *addcityVC = [AddCity new];
addcityVC.delegate = self; // initialize the delegate here
[self presentViewController:addCityVC animated:YES completion:nil];
You need to initialize the delegate back to self so it can call the method you need
Hope this helps

Delegate method not being called?

I have a view controller with a delegate method that should be called, but it doesn't?
NotifyingViewController.h
#protocol NotifyingViewControllerDelegate <NSObject>
#required
- (void)iWasAccepted;
#end
#interface NotifyingViewController : UIViewController
#property (nonatomic, weak) id<NotifyingViewControllerDelegate> delegate;
NotifyingViewController.m
-(void)someMethod{
[self.delegate iWasAccepted];
[self dismissViewControllerAnimated:YES completion:nil];
}
NotifiedViewController.h
#import "NotifyingViewController.h"
#interface NotifiedViewController : UIViewController <NotifyingViewControllerDelegate>
NotifiedViewController.m
-(void)iWasAccepted{
[self saveIntoDB];
NSLog(#"DELEGATE RAN");
}
For some reason, the controller that should be notified isn't. The Notifying controller does dismiss meaning the method that alerts the delegate IS run, but the delegate doesn't run the function because it doesn't NSLog. Any ideas why?
You can't just specify that an object conforms to a protocol. You must also assign that object as the delegate. When you alloc/init the instance of NotifyingViewController, set its delegate to self and you should be fine.
NotifyingViewController *notifyingInstance = [[NotifyingViewController alloc] init];
[notifyingInstance setDelegate:self];
It is important to both do this, and specify that the class conforms to the protocol, which you're already doing with this line.
#interface NotifiedViewController : UIViewController <NotifyingViewControllerDelegate>
Additionally, when calling delegate methods, it's good practice to wrap the function calls in respondsToSelector: checks.
if ([self.delegate respondsToSelector:#selector(iWasAccepted)]) {
[self.delegate iWasAccepted];
}

iOS - How to implement self created delegate in ios6

i have created a delegate for my project the code of my main view is
VedantViewController.h
#protocol VedantDelegate;
#interface VedantViewController : UIViewController
{
id <VedantDelegate> delegate;
}
//some other outlets
#property(nonatomic, assign) id <VedantDelegate> delegate;
#protocol VedantDelegate <NSObject>
- (void)display:(NSString *)JSONResponse;
#end
VedantViewController.m
#synthesize delegate;
[delegate display:jsonResponse];
SecondViewController.h
#interface SecondViewController : UIViewController<VedantDelegate>
- (void)display:(NSString *)JSONResponse;
SecondViewController.m
- (void)display:(NSString *)string
{
}
but this code is not working properly
when i debug the code using breakpoints the code reaches the
[delegate display:abc];
but it does not calls display function in SecondViewController.m file
i think my code is right but some mistake that i can't recognize
let me explain you the flow of my project this could be the problem
by default the VedantViewController view is launched
after that when the show button is click it calls the SecondViewController view in the view these is list button that calls the function in VedantViewController this function then calls the delegate method that is [delegate display:jsonResponse];
Thanks in Advance,
Arun.
The view controller which is confirming with the protocol, should have this line in the viewDidLoad or anywhere you are making the object of that viewController
Add this line in SecondViewController.m
VedantViewControllerObject.delegate = self;
#protocol VedantDelegate;
#interface VedantViewController : UIViewController{
id<VedantDelegate> delegate;
}
//some other outlets
#property(nonatomic,assign) id<VedantDelegate> delegate;
#protocol VedantDelegate <NSObject>
-(void)displayAccounts:(NSString *)JSONResponse;
-(void)display:(NSString *)JSONResponse;
#end
and also set delegate to object of VedantViewControllerObject class as self in SecondViewController class and the object of VedantViewControllerObject class should be initialized and allocated.
vedantViewControllerObject.delegate = self;
In your VedantViewController.h file you declared method as below
-(void)displayAccounts:(NSString *)JSONResponse;
But you are calling it [delegate display:jsonResponse];
You just try to call
[delegate displayAccounts:jsonResponse];
And in SecondViewController.m
(void)displayAccounts:(NSString *)string{
}
There are some issues in your code:
set the delegate in second view controller
vedViewObject.delegate = self;
You added displayAccounts method in delegate and calling display method, that can cause issues. If that methods are not implemented in the delegate class.
Add if condition like: if(delegate)[delegate displayAccounts:jsonResponse];

Resources