繁体   English   中英

在ContainerView中显示ViewController

[英]Showing a ViewController in a ContainerView

我正在尝试在ContainerView中显示ViewController。 如果使用内部ViewController(与ContainerView在同一项目中的源代码),它将按预期工作。 所以我在另一个项目中使用ViewController,因此不会显示。 我已经在外部ViewController的viewDidLoad中实现了AlertDialog,并且将显示AlterDialog。

编辑:我发现我必须在构建阶段(在主项目中)将外部ViewController的.xib添加到复制捆绑资源中。 还有另一种解决此问题的方法吗?

码:

#import "ViewController.h"
#import "Utilities/Form.h"
#import "TestForm.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize ListContainer = _ListContainer;

- (void)viewDidLoad {
    [super viewDidLoad];
    @try {

        Form *viewConnection = [[Form alloc]init];
        viewConnection.view.frame = _ListContainer.bounds;
        [_ListContainer addSubview:viewConnection.view];
        [self addChildViewController:viewConnection];
        [viewConnection didMoveToParentViewController:self];
    }
    @catch (NSException *exception) {
            }
    @finally {


    }
}

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

@end

alloc / init几乎总是错误的方式来创建视图控制器。 它不提供包含视图内容的XIB文件或情节提要。 您可能应该在Form类中提供一个自定义的init方法,您可以调用该方法,该方法使用instantiateViewControllerWithIdentifier从另一个项目的故事板上创建视图控制器,或者使用initWithNibName:bundle:用笔尖创建视图控制器。

终于明白了!
就像Duncan C提到的那样,我必须编写自己的init。
以下代码是结果:

//
//  Form.m
//  Utilities

#import "Form.h"

@interface Form ()

@end

@implementation Form

-(id)init
{
    NSBundle* resourcesBundle = [NSBundle bundleForClass:[Form class]];
    self = [super initWithNibName:@"Form" bundle:resourcesBundle];
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM