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

标签 ios objective-c uitableview ios7

我想以编程方式创建一个 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/

相关文章:

ios - 设置标签字体为粗体和斜体

objective-c - AFNetworking 中的 ASIFormDataRequest?

objective-c - 在IOS中发出http请求并从返回的json对象中解析特定数据

c++ - Stringstream 缓冲区在 Objective-C++ 中不会为空

ios - 无法从 didSelectRowAtIndexPath 更改附件类型?

ios - Swift:使用未解析的标识符 'present'

ios - 检测用户是否在 iTunes 商店中登录

objective-c - 同时 NSURL 下载

ios - 如何在tableview和viewcontroller之间传递字符串

ios - UITableView 自定义部分页脚