我正在尝试使用 addChildViewController
功能在其他 UIViewcontroller
之上显示一个 UIViewController
。childViewController 是一个 显示的 tableView 在我的 MainViewController 之上,但是我没有看到它拥有的 TableView 。如果我单独执行 childViewController,tableView 工作正常,那么我在这里缺少什么。 p>
下面是我添加 childVC 的方式:
@implementation Test2ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)showChildVC:(id)sender
{
TestTableViewController *tVC = [[TestTableViewController alloc]init];
tVC.view.frame = CGRectMake(50, 50, 200, 200);
[self addChildViewController:tVC];
[self.view addSubview:tVC.view];
[tVC didMoveToParentViewController:self];
}
这是我要展示的 childVC:.h
#import <UIKit/UIKit.h>
@interface TestTableViewController : UIViewController<UITableViewDataSource>
{
NSArray *array;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
和:.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
array = [NSArray arrayWithObjects:@"One",@"Two",@"Three",@"Four", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
最佳答案
我在第二个 View Controller 中看到你的 TableView 是一个 IBOutlet,所以你将它放在 Storyboard 中。
然后当你实例化它时,你不能做:[[TestTableViewController alloc]init];
你必须做:
[storyBoard instantiateViewControllerWithIdentifier:@"tVCStoryBoardID"];
关于ios - 在 iOS 中切换 UIViewControllers 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21398781/