2

我有一个包含视图控制器类的静态库。

在包含静态库(和头文件)之后,如何在 Xcode 中的新项目中调用此视图控制器?

在静态库上查看控制器:

#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];

我还缺少其他东西吗?

*更新

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

4

2 回答 2

1

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

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

最后一种方法是将它作为一个 bundle 包含在你的项目中。与上述过程相比,这是一个有点复杂且不直观的过程。

编辑:

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

于 2013-10-15T19:40:46.013 回答
-1

我错过了视图控制器的基本结构。

在 viewController 中创建一个 UIWindow 为我解决了这个问题。

完整源代码:

静态库视图控制器文件(VC.m):

#import "VC.h"

@interface VC ()
    @property (nonatomic, strong) UILabel *label;
    @property (nonatomic, strong) UIButton *button;
    @property (nonatomic, strong) UIView *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
{
    [super viewDidLoad];
// CREATE UIVIEW INSIDE VIEW CONTROLLER
    _window = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _window.backgroundColor = [UIColor whiteColor];
// CREATE BUTTON TO CLOSE VIEW CONTROLLER AFTER IS SHOWN
    _button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [_button addTarget:self action:@selector(closeWindow:) forControlEvents:UIControlEventTouchDown];
    [_button setTitle:@"Close View" forState:UIControlStateNormal];
    _button.frame = CGRectMake(115.0f, 150.0f, 200.0f, 30.0f);
// ADD BUTTON TO THE UIWINDOW
    [_window addSubview: _button];
// ADD UIWINDOW TO VIEW CONTROLLER
    [self.view addSubview: _window];

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Close ViewController
- (IBAction)closeWindow:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end

将静态库和标头导入新应用程序后,您可以使用 IBAction 调用视图控制器,如下所示:

#import "VC.h" //header for viewcontroller code in static library
...
-(IBAction)callVC:(id)sender {
    VC *vc = [[VC alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}
于 2013-10-15T23:01:13.807 回答