简体   繁体   English

以编程方式加载UITableViewController并将视图添加为子视图

[英]Load UITableViewController programmatically and add view as subview

I want to load a UITableViewController inside a UIView because I want to change the view on button click (like UITabBar but with my own buttons). 我想在UIView中加载UITableViewController,因为我想在单击按钮时更改视图(如UITabBar,但有我自己的按钮)。 I'm using a storyboard and have defined a TableViewController with custom class "InitialTableViewController" and identifier "InitialView". 我正在使用情节提要,并使用自定义类“ InitialTableViewController”和标识符“ InitialView”定义了TableViewController。

My code look like this: 我的代码如下所示:

#import "MyViewController.h"
#import "InitialTableViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    InitialTableViewController *tableControl = [self.storyboard instantiateViewControllerWithIdentifier:@"InitialView"];

    [[self view] addSubview:[tableControl view]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

@end

The view starts and I can see my table but the code inside "InitialTableViewController" doesn't work. 视图开始,我可以看到我的表,但是“ InitialTableViewController”中的代码不起作用。

What can I do? 我能做什么?

Well, it would be easier to just have a normal UIViewController with a UIView as root of the Nib and then put a UITableView . 好吧,只需要一个普通的UIViewController并将UIView作为Nib的根,然后放置一个UITableView会更容易。 My answer is based on your need to have buttons on that UIView . 我的答案是基于您需要在该UIView上具有按钮。

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

相关问题 将 UIView 作为子视图添加到 UITableViewController 的视图 - Add a UIView as a subview to a UITableViewController's view 以编程方式将UITableViewController添加到当前视图 - Add UITableViewController to Current View Programmatically 添加故事板视图作为以编程方式创建的视图的子视图 - Add a storyboard view as a subview of a programmatically created view Swift-以编程方式将自定义Xib视图添加为子视图 - Swift - Add Custom Xib View As Subview Programmatically 将可点击和固定的子视图添加到UITableViewController? - Add clickable and fixed subview to UITableViewController? 如何以编程方式向我的UITableViewController添加活动指示器视图? - How do I add an activity indicator view to my UITableViewController programmatically? 将UITableViewController.view添加为表现不佳的子视图 - Adding UITableViewController.view as subview behaving badly 我应该在哪里将子视图添加到UITableViewController中的表视图下? - Where should I add my subview to have it UNDER my table view in a UITableViewController? 如何以编程方式添加子视图 - How to programmatically add a subview 如何以编程方式在情节提要子视图后面添加子视图 - how to add subview programmatically behind storyboard sub view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM