在其他实现IOS中调用UITABLEVIEW



我有一个名为MasterViewController的视图控制器,其中实现了tableView(作为我的模块的一部分)。它包含以下方法:

- (void)viewDidLoad
- (void)didReceiveMemoryWarning
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

现在我想将我的模块集成到名为"myProject"的主项目中,其视图控制器ViewController

我怎样才能init ViewController MasterViewController,即调用其init方法。我目前的方法只是加载(void)viewDidLoad方法并在我的ViewController中显示空表视图,同时考虑到我的MasterViewController有一个名为Mainstoryboard接口生成器

我的方法:

 MasterViewController* mainVC = [[MasterViewController alloc] init]; 
            [self.view insertSubview:mainVC.view belowSubview:tabBar]; 

你可以做的是在viewDidLoad of ViewController 类中添加以下内容;

在 Viewcontroll.h 中:

- (void)viewDidLoad
{
 ..... 
    MasterViewController *MSVC = (MasterViewController * )[self.storyboard instantiateViewControllerWithIdentifier:@"Main"];
   [self addChildViewController:MSVC];
   [self.view addSubview:MSVC.view];
.....
}

请注意,您必须同时调用 addChildViewController 和 addSubview 才能显示子视图。您不应该在 ViewController 的 init 方法中初始化 MasterViewController。

最新更新