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.
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. 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. 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"];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.