在ContainerView中显示ViewController

时间:2015-10-28 21:40:12

标签: ios objective-c uiviewcontroller

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

编辑: 我发现我必须将外部ViewController的.xib添加到Build Phases中的Copy Bundle Resouces(在主项目中)。 有没有其他方法可以解决这个问题?

代码:

#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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

终于搞定了!
就像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