gpt4 book ai didi

ios - 以编程方式创建 UITableViewController 时使用 Storyboard

转载 作者:行者123 更新时间:2023-11-29 02:54:35 28 4
gpt4 key购买 nike

我想以编程方式创建一个 UITableViewController,使用我在 Storyboard中创建的 TableView,我将 Storyboard命名为 TableViewCont.storyboard

这是我试图在 AppDelegate.m 中使用的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSBundle *bundle = [NSBundle mainBundle];
UITableViewController *tvc = [[UITableViewController alloc] initWithNib:@"TableViewCont" bundle:bundle];
self.window.rootViewController = tvc;
return YES;
}

没有语法错误报告,但当我在我的设备上运行该应用程序时,TableViewController 没有显示。当您使用 initWithNib:bundle: 初始化时,我知道 UITableViewController 的功能与 UIViewController 不同,但我不知道为什么这段代码不起作用...

最佳答案

您不能在 Storyboard中使用 initWithNib。这是针对 xib 文件的。

要从 Storyboard初始化 Controller ,您必须使用 instantiateViewControllerWithIdentifier。看下面的例子

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TableViewCont" bundle:nil]; 

UITableViewController *tvc = (UITableViewController *)[storyboard instantiateViewControllerWithIdentifier:@"your identifier"];

不要忘记在 Storyboard中为您的 tableview Controller 添加标识符

关于ios - 以编程方式创建 UITableViewController 时使用 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24073895/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com