如何使用Xib文件创建UIViewController? [英] How do I create UIViewController using Xib-file?
问题描述
很抱歉,如果这个问题已经得到解答,但我找不到答案。
Sorry if this question has already been answered but I can't find an answer.
我正在创建一个应用程序,其中我有一个UITableViewController,当选择单元格右侧的附件按钮,应创建一个包含在.xib文件中找到的接口的UIViewController的新实例。然后,新的UIViewController将被推送到堆栈并显示。
我的问题是如何从现有的.xib文件创建新的UIViewController?
I'm creating an app in which I have an UITableViewController and when the accessorybutton in the right side of a cell is selected a new instance of a UIViewController should be created containing the interface found in a .xib-file. The new UIViewController will then be pushed on to the stack and displayed. My question is how do I create the new UIViewController from an existing .xib-file?
这是我到目前为止所尝试的:
在Xcode:File - > New File - > Cocoa Touch Class - > UIViewController subClass。
复选框UITableViewController子类未选中。
复选框使用XIB进行用户界面选中。
This is what I have tried so far: In Xcode: File -> New File -> Cocoa Touch Class -> UIViewController subClass. Checkbox "UITableViewController subclass" unchecked. Checkbox "With XIB for user interface" checked.
这将创建.m,.h和.xib文件。我在.xib文件的视图中创建了一个用户界面。在界面构建器中选择文件所有者会在类标识中显示新创建的UIViewController。
This creates a .m, a .h and a .xib file. I created a user interface in the "view" in the .xib-file. Selecting "File's owner" in interface builder shows the newly created UIViewController in "Class Identity".
一些代码:
在DetailViewController.m(新的UIViewController)中:
In DetailViewController.m (the new UIViewController):
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
在SubViewController.m(旧的UITableViewController)中:
In SubViewController.m (the old UITableViewController):
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
//Exception thrown at line below
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
忘记提及.xib文件的名称是DetailViewController.xib 。
Forgot to mention that the .xib-file's name is "DetailViewController.xib".
代码编译正常,但是当我在模拟器中运行它并按下一个附件按钮时,它会因未捕获的异常而终止。
The code compiles fine but when I run it in the simulator and press an accessorybutton it terminates due to uncaught exception.
我缺少什么?
提前致谢
推荐答案
错误出现在我试图显示的新视图中。在.xib文件中,我插入了一个MKMapView,当我删除它时,一切都运行良好。
The error was in the new view I was trying to display. In the .xib-file I had inserted a MKMapView and when I removed this it all works perfectly.
再次感谢您帮我调试!
这篇关于如何使用Xib文件创建UIViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!