ios - Objective-C 中的路由设计模式
问题描述
我在 Swift 上找到了关于路由设计模式的好教程
有人可以解释它或用Objective-C创建它吗?
目标
从一个视图控制器跳转到任何其他视图,反之亦然
意味着用户可以使用此模式从任何视图跳转到任何视图
https://medium.com/commencis/routing-with-mvvm-on-ios-f22d021ad2b2
解决方案
对于这种流,我使用流协调器。这只是意味着在视图控制器本身中,您没有硬编码的目标视图控制器,您只需调用一些委托(我称他为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
被分配。
推荐阅读
- javascript - 如何通过 JS 或其他浏览器自动化工具更改打开页面的 url
- firebase - 无法将颤动的 Web 应用程序链接到 Firebase 项目/无限加载
- html - 如何在文本里面创建一个选项卡
- plaid - 格子 - 更新模式与重新关联帐户
- c++ - 用于获取必须在 png_struct->num_rows 和 png_struct->flush_rows 中的数据的 C++ 函数
- javascript - Google Maps JavaScript API 请求被拒绝
- python - python:解析冒号分隔的格式化字符串
- c# - 如何将委托和显示设置到控制台(C#)中?
- django - ckeditor如何更改按钮文本
- c - 反转字符串中单词顺序的程序错误地打印反转的字符串