[英]Delegate function not called in AppDelegate
我在视图控制器中编写了协议,并在AppDelegate中实现它,当我从视图控制器调用委托函数时,不调用委托函数。 以下是我的代码 -
在类AuthenticationViewController中 -
@class AuthenticationViewController;
@protocol ApplicationTiomeoutDelegate <NSObject>
-(void) addObserverForTimeout;
@end
并使用委托调用此函数 -
[self.appTimeoutDelegate addObserverForApplicationTimeout];
在AppDelegate中,我已经像这样实现了这个协议 -
@interface AppDelegate () <ApplicationTiomeoutDelegate>
@end
然后将代表设置为自我 -
AuthenticationViewController *timeoutDelegate = [[AuthenticationViewController alloc] init];
[timeoutDelegate setAppTimeoutDelegate:self];
并在AppDelegate中实现了委托功能,从未以某种方式调用它 -
-(void) addObserverForApplicationTimeout{
// this function is never called
}
我不确定这里有什么不对。
作为单例的 AppDelegate
不需要具有ApplicationTiomeoutDelegate
协议调用。 您可以直接调用addObserverForTimeout
您可以在appdelegate中创建方法,您可以使用appdelegate实例直接在任何地方调用它。
对于您的问题,请查看以下内容
在哪里为您的委托创建实例以及在哪里从您的viewcontroller调用您的委托,它用于在两个类之间进行通信,它应该具有协议的引用。
尝试这个
你的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];
}
你的app代表
@interface AppDelegate () <customDelegate>
@end
你的完成了
viewController = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
viewController.delegate = self;
并实现方法:
-(void)method{
NSLog(@"calling");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.