简体   繁体   English

在AppDelegate中未调用委托函数

[英]Delegate function not called in AppDelegate

I have written protocol in a view controller, and implement it in AppDelegate, and when I call delegate function from view controller, the delegate function is not called. 我在视图控制器中编写了协议,并在AppDelegate中实现它,当我从视图控制器调用委托函数时,不调用委托函数。 Below is my code - 以下是我的代码 -

In class AuthenticationViewController - 在类AuthenticationViewController中 -

@class AuthenticationViewController;
@protocol ApplicationTiomeoutDelegate <NSObject>

-(void) addObserverForTimeout;

@end

And call this function using delegate - 并使用委托调用此函数 -

[self.appTimeoutDelegate addObserverForApplicationTimeout];

And in AppDelegate, I have implemented this protocol like this - 在AppDelegate中,我已经像这样实现了这个协议 -

@interface AppDelegate () <ApplicationTiomeoutDelegate>
@end

And then set delegate to self - 然后将代表设置为自我 -

AuthenticationViewController *timeoutDelegate = [[AuthenticationViewController alloc] init];
[timeoutDelegate setAppTimeoutDelegate:self]; 

And implemented delegate function as well in AppDelegate, which is never called somehow - 并在AppDelegate中实现了委托功能,从未以某种方式调用它 -

-(void) addObserverForApplicationTimeout{
 // this function is never called 
} 

I am not sure what is not correct here. 我不确定这里有什么不对。

AppDelegate being a singleton need not have the ApplicationTiomeoutDelegate protocol invocation. 作为单例的 AppDelegate不需要具有ApplicationTiomeoutDelegate协议调用。 You can directly invoke addObserverForTimeout 您可以直接调用addObserverForTimeout

You create method in your appdelegate , you can directly call it wherever you want with instance of appdelegate . 您可以在appdelegate中创建方法,您可以使用appdelegate实例直接在任何地方调用它。

For your question, check this below 对于您的问题,请查看以下内容

Where are creating instance for Your delegate and where are calling your delegate form your viewcontroller , it is used to communicate between two class, it should have reference of protocol. 在哪里为您的委托创建实例以及在哪里从您的viewcontroller调用您的委托,它用于在两个类之间进行通信,它应该具有协议的引用。

try this 尝试这个

Your viewcontroller.h 你的viewcontroller.h

@protocol customDelegate;
#import <UIKit/UIKit.h>
#import "CustomTableViewCell.h"

@interface ViewController : UIViewController<UITableViewDelegate>
{

    IBOutlet UITableView *mainTableView;
}
@property (nonatomic,strong) id <customDelegate> delegate;
@end

@protocol customDelegate <NSObject>

-(void)method;

@end

ViewController.m ViewController.m

- (void)viewDidLoad {
    [self.delegate method];
}

Your app delegate 你的app代表

@interface AppDelegate () <customDelegate>
@end

Your didfinishlaunghing 你的完成了

viewController = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    viewController.delegate = self;

and implement method: 并实现方法:

-(void)method{
    NSLog(@"calling");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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