1

我试图UIViewController在其他UIViewcontroller使用addChildViewController功能之上显示一个。childViewController 是一个 tableView,它显示在我的 MainViewController 之上,但是我看不到它具有的 table view。如果我单独执行 childViewController,tableView 工作正常,所以我在这里想念什么。

这是我添加 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;

       }
4

1 回答 1

1

我看到您在第二个视图控制器中的表视图是一个 IBOutlet,所以您将它放在 Storyboard 中。

然后当你实例化它时,你不能做:[[TestTableViewController alloc]init];你必须做:

[storyBoard instantiateViewControllerWithIdentifier:@"tVCStoryBoardID"];
于 2014-01-28T07:43:06.907 回答