ios - 在 iOS 中切换 UIViewControllers 不起作用

标签 ios objective-c uitableview uiviewcontroller uicontainerview

我正在尝试使用 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/

相关文章:

ios - 选择时自定义 UITableViewCell 更改?

ios - UITableView黑色/崩溃iOS 8-无日志

ios - IB导出 : Unexpectedly found nil while implicitly unwrapping an Optional value

php - 使用 PHP Response 来存储/使用数据

ios - 如何绘制一个矩形的角(没有连接它们的线)

ios - 在 iOS 中使用 Yelp api

ios - 在 Objective C 项目中使用 Swift Pod

iphone - 如何检测用户选择不允许用于 iphone 的 MKMapView

ios - 如何在 Object C 中使用 block 调用方法?

ios - 带有 ReactiveCocoa 的 MVVM : limit the text length of a UITextField in view model