[英]Custom Delegate Methods not being called
Look at the following code. 请查看以下代码。
In CusFormViewController.h
在
CusFormViewController.h
@interface CusFormViewController : CusBaseViewController
@protocol CusFormViewControllerDelegate <NSObject>
-(void)ticketCreated:(NSString*)ticketID;
-(void)ticketFormRenderingFinished;
@end
@property (nonatomic, weak) id<CusFormViewControllerDelegate> delegate;
In CusFormViewController.m
在
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
在
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. 没有调用
ticketCreated
和ticketFormRenderingFinished
。
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. ViewController
创建的CusFormViewController
对象与您在屏幕上显示的对象相同,并且委托是在同一对象上设置的。 I truly believe you are creating a new CusFormViewController
object and setting delegate on that. CusFormViewController
对象并在其上设置委托。 CusFormViewController
, before calling delegate, check the existence of delegate as well. CusFormViewController
,在调用委托之前, CusFormViewController
检查委托是否存在。 This is a safety check, you can also put a NSLog statement to double check if your delegate exists or not. ViewController
to CusFormViewController
then you set delegate in prepareForSegue:
and not in viewDidLoad
. ViewController
转到CusFormViewController
那么你在prepareForSegue:
设置委托prepareForSegue:
而不是在viewDidLoad
。 As a side note, put a NSLog
statement in viewDidLoad
of your CusFormViewController
and print self.delegate
to check the property setting. 作为旁注,在您的
CusFormViewController
viewDidLoad
中放置一个NSLog
语句并打印self.delegate
以检查属性设置。
Your controller formVC
is dealloced after the function viewDidLoad
executed. 执行函数
viewDidLoad
后,将formVC
分配控制器formVC
。 Create strong reference on your formVC for example like this: 在formVC上创建强大的引用,例如:
@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的实例
CusFormViewController * _formVC=[[UIStoryboard storyboardWithName:storyboardName bundle: nil]instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_ CusFormViewController"]; CusFormViewController * _formVC = [[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:@“IDENTIFIER_OF_YOUR_ CusFormViewController”];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.