gpt4 book ai didi

ios - 委托(delegate)方法不触发?

转载 作者:行者123 更新时间:2023-11-28 18:33:22 28 4
gpt4 key购买 nike

就在我以为我对委托(delegate)的工作方式有所了解时,似乎我遗漏了什么。

我已经像这样设置了一个委托(delegate):

ViewControllerA.h

@protocol LoginDelegate <NSObject>

-(void)userLoginSuccessful;

@end
@interface BBLoginViewController : BBBaseViewController
@property (weak, nonatomic) id <LoginDelegate> delegate;

@end

ViewControllerA.m

-(void)someMethod 
{
if ([self.delegate respondsToSelector:@selector(userLoginSuccessful)]){
[self.delegate userLoginSuccessful];
}
[self dismissViewControllerAnimated:YES completion:nil];

}

ViewControllerB.m

    #import "ViewControllerA.h"

@interface ViewControllerB () <UITableViewDataSource, UITableViewDelegate, LoginDelegate>


-(void)viewWillAppear:(BOOL)animated
{

ViewControllerA *loginViewController = [[ViewControllerA alloc]init];
loginViewController.delegate = self;

}

#pragma mark - Login Delegate

-(void) userLoginSuccessful
{
NSLog (@"Delegate fired!");
}

毕竟我的委托(delegate)方法没有触发。我究竟做错了什么?我看了this SO answer并且当检查委托(delegate)不是零时 - 它不是。它设置为 ViewControllerB

最佳答案

这段代码:

-(void)viewWillAppear:(BOOL)animated
{

ViewControllerA *loginViewController = [[ViewControllerA alloc]init];
loginViewController.delegate = self;

}

将创建一个 ViewControllerA 对象,然后(几乎)立即销毁它,这不是您想要的。

无论如何,您可能希望在 prepareForSegue: 方法中设置委托(delegate)...

关于ios - 委托(delegate)方法不触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23753688/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com