UISplitViewController以编程方式没有nib / xib [英] UISplitViewController programmatically without nib/xib
问题描述
我通常创建没有IB内容的项目。我做的第一件事是剥离所有对xibs,outlet更新plist等的引用。没有问题,效果很好(在我的世界里)!
I usually create my projects without IB-stuff. The first thing I do is to strip off all references to xibs, outlets updated plist, etc and so forth. No problems, works great (in my world)!
现在,我刚刚安装了3.2并尝试开发我的第一个iPad应用程序。按照与以前相同的步骤,我创建了一个基于UISplitView的应用程序项目,并剥离了所有IB的东西。另外,我按照Apple的参考文档中的部分进行了操作:以编程方式创建拆分视图控制器,但是,从不显示主视图,只显示详细视图(无论方向是什么)。我真的试着仔细看看这个,但我无法理解我错过了什么。
Now, I just installed 3.2 and tried to develop my first iPad app. Following same procedure as before, I created a UISplitView-based application project and stripped off all IB-stuff. Also, I followed the section in Apple's reference docs: Creating a Split View Controller Programmatically, but nevertheless, the Master-view is never shown, only the Detail-view is (no matter what the orientation is). I really have tried to carefully look this through but I cannot understand what I have missed.
是否有一个UISplitViewController的工作示例,没有笔尖漂浮在某处?我用谷歌搜索但找不到任何东西。或者你知道我可能错过了什么吗?
Is there a working example of a UISplitViewController without the nibs floating around somewhere? I have googled but could not find any. Or do you know what I probably have missed?
推荐答案
在你的委托标题中声明你的splitviewcontroller,在你的didfinishlaunching中使用这样的东西
Declare your splitviewcontroller in your delegate header, use something like this in your didfinishlaunching
确保将UISplitViewControllerDelegate添加到detailedViewController头文件中,并且您还拥有委托方法。记得导入相关的头文件
ensure you add the UISplitViewControllerDelegate to the detailedViewController header file and that you have the delegate methods aswell. remember to import relevant header files
- (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子视图,而是
EDIT - as per Scott's excellent suggestion below, don't add to the windows subview, instead
[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代码; - )
I also prefer code to IB ;-)
这篇关于UISplitViewController以编程方式没有nib / xib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!