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. Below is my code -
In class 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 -
@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 -
-(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. You can directly invoke addObserverForTimeout
You create method in your appdelegate , you can directly call it wherever you want with instance of 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.
try this
Your 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
- (void)viewDidLoad {
[self.delegate method];
}
Your app delegate
@interface AppDelegate () <customDelegate>
@end
Your didfinishlaunghing
viewController = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
viewController.delegate = self;
and implement method:
-(void)method{
NSLog(@"calling");
}
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.