简体   繁体   English

自定义委托方法未被调用

[英]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. 没有调用ticketCreatedticketFormRenderingFinished

Most common reason for delegate method not being called is dealing with incorrect objects. 委托方法未被调用的最常见原因是处理不正确的对象。

  1. 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. 确保从ViewController创建的CusFormViewController对象与您在屏幕上显示的对象相同,并且委托是在同一对象上设置的。 I truly believe you are creating a new CusFormViewController object and setting delegate on that. 我真的相信你正在创建一个新的CusFormViewController对象并在其上设置委托。
  2. In 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. 这是一个安全检查,你也可以放一个NSLog语句来仔细检查你的代表是否存在。 You are failing here. 你在这里失败了。
  3. If you are segueing from 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM