我通常在没有 IB 的情况下创建我的项目。我要做的第一件事是去掉所有对 xibs、网点更新 plist 等的引用。没问题,效果很好(在我的世界里)!
现在,我刚刚安装了 3.2 并尝试开发我的第一个 iPad 应用程序。按照与以前相同的过程,我创建了一个基于 UISplitView 的应用程序项目并剥离了所有 IB Material 。另外,我遵循了 Apple 引用文档中的部分:Creating a Split View Controller Programmatically ,但无论如何,主视图从未显示,只有细节 View (无论方向如何)。我真的尝试过仔细查看,但我无法理解我错过了什么。
是否有一个 UISplitViewController 的工作示例,没有 Nib 在某处 float ?我用谷歌搜索但找不到任何东西。或者你知道我可能错过了什么吗?
最佳答案
在您的委托(delegate) header 中声明您的 splitviewcontroller,在您的 didfinishlaunching 中使用类似的内容
确保您将 UISplitViewControllerDelegate 添加到 detailViewController 头文件中,并且您也拥有委托(delegate)方法。记得导入相关头文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
splitViewController = [[UISplitViewController alloc] init];
rootViewController *root = [[rootViewController alloc] init];
detailedViewController *detail = [[detailedViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:root];
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detail];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
splitViewController.delegate = detail;
[window addSubview:splitViewController.view];
编辑 - 根据 Scott 下面的极好建议,不要添加到 windows subview 中,而是
[self.window setRootViewController:(UIViewController*)splitViewController]; // that's the ticket
[window makeKeyAndVisible];
return YES;
}
//detailedView delegate methods
- (void)splitViewController:(UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController:(UIPopoverController*)pc
{
[barButtonItem setTitle:@"your title"];
self.navigationItem.leftBarButtonItem = barButtonItem;
}
- (void)splitViewController:(UISplitViewController*)svc
willShowViewController:(UIViewController *)aViewController
invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
self.navigationItem.leftBarButtonItem = nil;
}
我也更喜欢代码而不是 IB ;-)
关于objective-c - UISplitViewController 以编程方式没有 nib/xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2757489/