gpt4 book ai didi

ios - 如何从 Xcode 的静态库中打开 viewController?

转载 作者:行者123 更新时间:2023-12-01 16:46:37 26 4
gpt4 key购买 nike

我有一个包含 View Controller 类的静态库。

如何在 Xcode 中创建新项目 包含静态库后 (和标题)我可以调用这个 viewController 吗?

在静态库上查看 Controller :

#import "VC.h"

@interface VC ()
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIWindow *window;
@end


@implementation VC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.backgroundColor = [UIColor whiteColor];
[self.view addSubview: _window];
[super viewDidLoad];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(115.0f, 150.0f, 200.0f, 30.0f)];
self.label.text = @"hello world!";
[self.view addSubview:self.label];
// Do any additional setup after loading the view.
}

@end

我试图在一个新项目中分配这个 ViewController 并调用它......但不工作:
VC *vc = [[VC alloc] init];
[self presentViewController:vc animated:YES completion:nil];

我还缺少其他东西吗?

* 更新

在最后一次编辑之后,现在我可以看到 View Controller ,但它不在主视图 Controller 的顶部,加载后它变成黑色......有什么想法吗?

最佳答案

有几种方法可以做到这一点。我想说最简单的方法就是简单地将您的 viewControllers .xib 或 storyBoard 作为文件包含在库的文件夹中。如果没有充分的理由让用户无法访问它,那应该不是什么大问题。

另一种方法是使用 universal framework制作你的图书馆。这将在您编译它时为您返回两个框架,一个带有后缀 .framework,另一个带有后缀 .embeddedframework。后者将链接到您的资源。

最后的方法是include it in your project as a bundle .与上述过程相比,这是一个有点复杂且不直观的过程。

编辑:

如果您不使用 Storyboard 或 .xib 文件执行此操作,则需要实际分配和初始化 self.view。它不是从 Storyboard 中加载的,因此您需要自己加载。这就是这段代码的问题。

关于ios - 如何从 Xcode 的静态库中打开 viewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19389558/

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