【问题标题】:Import viewController from project to library in swift/objective-c在 swift/objective-c 中将 viewController 从项目导入库
【发布时间】:2020-11-30 09:50:29
【问题描述】:
我的应用程序有一些可重用模块/选项卡。我通过 Cocoapods 添加了库。我有“通用”模块,其中包含所有选项卡的通用元素,但有时需要大量依赖导入。
ExampleViewController 可以从“MyApp”导入到另一个库,例如 Wall?我通常从“Common”导入。
为什么#import <MyApp/ExampleViewController-Swift.h> 不起作用?
下面是我对这个 ViewController 的调用函数。如何使用捆绑包?
【问题讨论】:
标签:
ios
objective-c
swift
cocoapods
reusability
【解决方案1】:
好的,我看到没有答案。我自己找到的。
如果有人有类似的情况。答案是NSNotificationCenter。
添加到你想要打开的函数SettingsViewController:
@IBAction func userPhotoClicked(_ sender: Any) {
NotificationCenter.default.post(name: Notification.Name("profileSide"), object: nil) }
在这个 ViewController(用 Objective-c 编写)上添加观察者的动作:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(profileSide:) name:@"profileSide" object:nil];
并移除观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"profileSide" object:nil];
并在具有 AddObserver 和 RemoveObserver 的同一视图控制器上编写函数“profileSide”。
- (void)profileSide:(NSNotification *)notification {
SettingsViewController *vc = [[SettingsViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
一切顺利。