簡體   English   中英

在AppDelegate中未調用委托函數

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM