[英]In UISplitViewController, can't make showDetailViewController:sender: push onto detail navigationController
In iOS 8, view controllers can now call showDetailViewController:sender:
to have the system determine the proper view controller to present the detail view controller. 在iOS 8中,视图控制器现在可以调用
showDetailViewController:sender:
让系统确定正确的视图控制器来呈现详细视图控制器。
In my app, I have a UISplitViewController, which contains two UINavigationControllers in its viewControllers array. 在我的应用程序中,我有一个UISplitViewController,它的viewControllers数组中包含两个UINavigationControllers。 The first UINavigationController contains my 'master' view, a subclass of UITableViewController.
第一个UINavigationController包含我的“主”视图,UITableViewController的子类。 The second UINavigationController contains my 'detail' view.
第二个UINavigationController包含我的“详细信息”视图。
Since I'm trying to make this work universally, I'm trying to use showDetailViewController:sender:
to display the detail view: 由于我试图使这项工作普遍,我正在尝试使用
showDetailViewController:sender:
来显示详细信息视图:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
[self showDetailViewController:self.itemVC sender:self];
}
This works fine with the Horizontal Compact trait (iPhone style), when self.splitViewController.collapsed == YES
, but not when the trait is Regular (iPad, not collapsed). 当
self.splitViewController.collapsed == YES
,这适用于Horizontal Compact特征(iPhone风格),但当特征是Regular(iPad,未折叠)时则不行。 On the iPad, it replaces the detail UINavigationController with the bare detail view controller (instead of replacing that UINavigationController's viewControllers array). 在iPad上,它用细节视图控制器替换细节UINavigationController(而不是替换UINavigationController的viewControllers数组)。
To get around this, I'm tested for whether or not it's collapsed, and if it isn't, I'm wrapping the detail view controller in another UINavigationController before showing it: 为了解决这个问题,我已经测试了它是否已经崩溃,如果不是,我会在显示它之前将详细视图控制器包装在另一个UINavigationController中:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
UIViewController *vcToShow;
// For whatever reason, when not collapsed, showDetailViewController replaces the detail view, doesn't push onto it.
if (self.splitViewController.collapsed) {
vcToShow = self.itemVC;
} else {
vcToShow = [[UINavigationController alloc] initWithRootViewController:self.itemVC];
}
[self showDetailViewController:vcToShow sender:self];
}
I suppose alternatively I could just configure self.itemVC
and avoid calling showDetailViewController:sender:
altogether when self.splitViewController.collapsed == NO
: 我想或者我可以配置
self.itemVC
并避免在self.splitViewController.collapsed == NO
时调用showDetailViewController:sender:
self.splitViewController.collapsed == NO
:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
// For whatever reason, when not collapsed, showDetailViewController replaces the detail view, doesn't push onto it.
if (self.splitViewController.collapsed) {
[self showDetailViewController:vcToShow sender:self];
}
}
But, this feels like it's defeating the purpose of showDetailViewController:sender:
, which is to loosen up the coupling between self
and the rest of the view hierarchy. 但是,这感觉就像它
showDetailViewController:sender:
的目的,即放松self
和视图层次结构的其余部分之间的耦合。
Is there a better way to handle this? 有没有更好的方法来处理这个?
In showDetailViewController:sender:
depending on the collapse
property you need to create the controller you want to show in the detail. 在
showDetailViewController:sender:
根据collapse
属性,您需要创建要在详细信息中显示的控制器。
Eg On the iPad in landscape mode it would already create the detail view controller from the storyboard but on the iPhone 5 where it is collapsed the view controller does not exist yet. 例如,在横向模式的iPad上,它已经从故事板创建了详细视图控制器,但在iPhone 5上折叠时,视图控制器尚不存在。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UINavigationController *detail;
ImageViewController *imageVC;
// on the iPhone (compact) the split view controller is collapsed
// therefore we need to create the navigation controller and its image view controllerfirst
if (self.splitViewController.collapsed) {
detail = [[UINavigationController alloc] init];
imageVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ImageViewController"];
[detail setViewControllers:@[imageVC] animated: NO];
}
// if the split view controller shows the detail view already there is no need to create the controllers
else {
id vc = self.splitViewController.viewControllers[1];
if ([vc isKindOfClass:[UINavigationController class]]) {
detail = (UINavigationController *)vc;
imageVC = [detail.viewControllers firstObject];
}
}
[self prepareImageViewController:imageVC forPhoto:self.photos[indexPath.row]];
// ask the split view controller to show the detail view
// the controller knows on iPhone and iPad how to show the detail
[self.splitViewController showDetailViewController:detail sender:self];
}
I hope this solves your issue. 我希望这能解决你的问题。
The way You doing it have a problem. 你这样做的方式有问题。 If your rotate the device(change the mode from collapsed to allVisible) after you select, you will find the detail vc without a navigation controller.
如果在选择后旋转设备(将模式从折叠更改为allVisible),您将找到没有导航控制器的详细信息vc。
If you call showDetailViewController:sender:
in all cases and pass the view controller with a navigation controller it will work fine in both cases and also will fix the rotaion problem mentioned above. 如果你在所有情况下调用
showDetailViewController:sender:
并使用导航控制器传递视图控制器,它将在两种情况下正常工作,并且还将修复上面提到的旋转问题。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
UIViewController *vcToShow= [[UINavigationController alloc] initWithRootViewController:self.itemVC];
[self showDetailViewController:vcToShow sender:self];
}
if (self.splitViewController.collapsed)
[self.splitViewController showDetailViewController:self.itemVC sender:self];
else
self.splitViewController.preferredDisplayMode = UISplitViewControllerDisplayModePrimaryHidden;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.