首页 > 解决方案 > Objective-C 中的路由设计模式

问题描述

我在 Swift 上找到了关于路由设计模式的好教程

有人可以解释它或用Objective-C创建它吗?

目标

从一个视图控制器跳转到任何其他视图,反之亦然

意味着用户可以使用此模式从任何视图跳转到任何视图

https://medium.com/commencis/routing-with-mvvm-on-ios-f22d021ad2b2

标签: iosobjective-ciphone

解决方案


对于这种流,我使用流协调器。这只是意味着在视图控制器本身中,您没有硬编码的目标视图控制器,您只需调用一些委托(我称他为flowDelegate),该委托决定下一步做什么。示例调用看起来像这样

对象-C:

@class HomeViewController;

@protocol HomeFlowDelegate

- (void)didTapRegistrationButtonInViewController:(HomeViewController*)viewController;

@end

@interface HomeViewController: UIViewController

@property (nonatomic,weak) id<HomeFlowDelegate> flowDelegate;

@end

@implementation HomeViewController

- (void)registrationButtonTapped {
    [self.flowDelegate didTapRegistrationButtonInViewController:self];
}

@end

迅速:

protocol HomeFlowDelegate {
    func didTapRegistrationButton(in viewController: HomeViewController)
}

class HomeViewController: ViewController {
    weak var flowDelegate: HomeFlowDelegate?

    func registrationButtonTapped() {
        flowDelegate?.didTapRegistrationButton(in: self)
    }
}

流程代表然后可以决定是否推送新屏幕或以模态方式呈现它或执行任何合适的流程。

这种方法意味着视图控制器是独立的,可以在应用程序的任何地方重用。您只需要确保当视图控制器出现在屏幕上时,它已flowDelegate被分配。


推荐阅读