2015-10-28 48 views
0

我試圖在ContainerView中顯示ViewController。 如果我使用內部ViewController(與ContainerView在同一個項目中的源代碼),它將按預期工作。 所以我從另一個項目中使用ViewController它不會顯示。 我已經在外部ViewController的viewDidLoad中實現了一個AlertDialog,並且將顯示AlterDialog。在ContainerView中顯示ViewController

編輯: 我發現我必須將外部ViewController的.xib添加到Build Phase中的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 

回答

0

分配/初始化幾乎總是錯誤的方式創建一個視圖控制器。它不提供包含視圖內容的XIB文件或故事板。您應該可以在您的Form類中提供一個可以調用的自定義init方法,該方法使用instantiateViewControllerWithIdentifier從另一個項目的故事板或initWithNibName:bundle:創建視圖控制器以使用筆尖創建它。

+0

感謝您的快速回復,但你的解決方案也不會太出。我問自己,如果我使用內部表單,爲什麼它可以工作,但是如果我使用外部表單,它不起作用 – bengt91

+0

「內部表單」? 「外部形式」?這些術語意味着什麼? –

+0

內部形式是我的主要項目中的視圖控制器外部的一個是在包含的框架項目內的視圖控制器 – bengt91

0

終於明白了!
像鄧肯C提到我必須寫我自己的初始化。
下面的代碼是結果:

// 
// 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 
相關問題