gpt4 book ai didi

ios - 从 Storyboard 中展开可防止委托(delegate)被调用

转载 作者:行者123 更新时间:2023-11-29 11:42:02 26 4
gpt4 key购买 nike

我有一个 UIViewController 几个 segues 深,当使用完成时,应该 unwind 并将它们带回 DashboardViewController。

我在仪表板中创建了 unwindToDashboard 方法,并在我的 FinishViewController 中将一个按钮连接到 Exit。这样当它被点击时就会触发展开 Action 。

这很好。

但我需要将数据从 FinishViewController 传回 Dashboard。

所以我为 FinishViewController 创建了一个委托(delegate) ProcessDataDelegate 并使 Dashboard 符合它。

但是,仪表板中的委托(delegate)方法被调用。

谁能告诉我我做错了什么?

DashboardViewController.m

#import "FinishViewController.h"

@interface DashboardViewController () <ProcessDataDelegate>{
FinishViewController *fm;
}
@end

@implementation DashboardViewController

- (void)viewDidLoad {
[super viewDidLoad];

if(!fm){
fm = [[FinishViewController alloc] init];

}

fm.delegate = self;
}

- (IBAction)unwindToDashboard:(UIStoryboardSegue *)unwindSegue {
//empty
}

#pragma mark PROTOCOL METHODS

-(void) didFinishWithResults:(NSDictionary*) dictionary{
NSLog(@"Dashboard method called didFinishWithResults");
}

@end

FinishViewController.h

@class FinishViewController;
@protocol ProcessDataDelegate <NSObject>
@required
- (void) didFinishWithResults: (NSDictionary*)dictionary;
@end

@interface FinishViewController : UIViewController
@property (nonatomic, weak) id <ProcessDataDelegate> delegate;
@end

FinishViewController.m

@interface FinishViewController () 
@end

@implementation FinishViewController

- (void)viewDidLoad {
[super viewDidLoad];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"fired via the button to the exit--- Segue: %@", [segue identifier]);
[delegate didFinishWithResults:nil ];
}

@end

最佳答案

您需要在 DashboardViewControllerprepareForSegue 方法中传递您的委托(delegate),在那里获取 destinationViewController 并转换为 FinishViewController 如果标识符等于你期望的 segue 标识符

像这样

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
NSLog(@"fired via the button to the exit--- Segue: %@", [segue identifier]);
if([[segue identifier] isEqualToString:@"YourSegueIdentifier"])
{
((FinishViewController*)[segue destinationViewController]).delegate = self
}
}

关于ios - 从 Storyboard 中展开可防止委托(delegate)被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45968770/

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