我試着在單個視圖內顯示多個UIViewController對象。目前我想在應用程序加載時顯示單個UIViewController對象。但是應用程序屏幕顯示爲空白,而應該在子視圖控制器內顯示一個標籤。iOS:UIViewController不顯示使用故事板的另一個UIViewController
這裏是我做過什麼:
ParentViewController.h
#import <UIKit/UIKit.h>
@interface ParentViewController : UIViewController
{
UIViewController *child1Controller;
UIViewController *child2Controller;
}
@end
ParentViewController.m
#import "ParentViewController.h"
#import "Child1Controller.h"
#import "Child2Controller.h"
@interface ParentViewController()
@end
@implementation ParentViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { ... }
- (void)viewDidLoad
{
child2Controller = [[Child2Controller alloc] init];
[self.view addSubview:child2Controller.view];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload { ... }
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { ... }
@end
然後在Interface Builder故事板
- 添加3視圖控制器
- 爲其中的每一個分配了一個類ParentViewController,Child1Controller & Child2Controller
- 在Child2Controller對象中,在View中添加了一個UILabel。在Child2Controller.h
- 定義的IBOutlet中進行的UILabel和Child2Controller.m 增加了合成語句相同
- 最後在項目的Info.plist設置的主要故事板文件
難道我在想念的東西這裏?
+1這確實工作得很好。但是我想知道的是我在問題中發佈的代碼/流程中所做的錯誤。 – vikmalhotra 2012-04-26 02:21:57
@ShiVik那麼Child2Controller呢?它是用xib加載還是覆蓋* loadView *方法? – 2012-04-26 07:46:44
不,我沒有創建一個新的xib文件,也沒有'loadView'。我所做的是我在故事板中添加了一個UIViewController,並在身份檢查器中將其自定義類分配爲「Child2Controller」。 – vikmalhotra 2012-04-26 08:12:16