简体   繁体   English

在iOS中切换UIViewControllers不起作用

[英]Switching UIViewControllers in iOS not working

I am trying to show a UIViewController on top of other UIViewcontroller using addChildViewController functionality.The childViewController is a tableView which shows up on top of my MainViewController however I do not see the table view that it has.If I execute the childViewController separately , the tableView works fine , so what am I missing here. 我试图使用addChildViewController功能在其他UIViewcontroller上显示一个UIViewController是一个显示在MainViewController顶部的tableView,但是我看不到它具有的表视图 。如果我单独执行childViewController,则tableView工作正常,所以我在这里缺少什么。

Here's how I am adding a childVC: 这是我添加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];
 }

And this is the childVC that I want to show: .h 这是我要显示的childVC:.h

     #import <UIKit/UIKit.h>

     @interface TestTableViewController : UIViewController<UITableViewDataSource>
     {
        NSArray *array;
     }
       @property (weak, nonatomic) IBOutlet UITableView *tableView;

      @end

And: .m 和:.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;

       }

I see your table view in the second view controller is an IBOutlet, so you are placing it in Storyboard. 我在第二个视图控制器中看到您的表视图是IBOutlet,因此您将其放置在Storyboard中。

Then when you instantiate it, you can't do: [[TestTableViewController alloc]init]; 然后,当您实例化它时,您将无法执行以下操作: [[TestTableViewController alloc]init]; you have to do: 你必须做:

[storyBoard instantiateViewControllerWithIdentifier:@"tVCStoryBoardID"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM