Jef*_*f V 18 objective-c uinavigationcontroller uisplitviewcontroller ios ios8
在iOS 8中,视图控制器现在可以调用showDetailViewController:sender:以让系统确定正确的视图控制器以呈现详细视图控制器.
在我的应用程序中,我有一个UISplitViewController,它的viewControllers数组中包含两个UINavigationControllers.第一个UINavigationController包含我的"主"视图,UITableViewController的子类.第二个UINavigationController包含我的"详细信息"视图.
因为我试图使这项工作普遍,我试图showDetailViewController:sender:用来显示详细信息视图:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.itemVC.item = self.itemStore.items[indexPath.row];
[self showDetailViewController:self.itemVC sender:self];
}
Run Code Online (Sandbox Code Playgroud)
这适用于水平紧凑特性(iPhone风格)时self.splitViewController.collapsed == YES,但不适用于特性为常规(iPad,未折叠)时.在iPad上,它用细节视图控制器替换细节UINavigationController(而不是替换UINavigationController的viewControllers数组).
为了解决这个问题,我已经测试了它是否已经崩溃,如果不是,我会在显示它之前将详细视图控制器包装在另一个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];
}
Run Code Online (Sandbox Code Playgroud)
我想或者我可以配置self.itemVC并避免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];
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这感觉就像它破坏了目的showDetailViewController:sender:,即放松self视图层次结构之间的耦合.
有没有更好的方法来处理这个?
在showDetailViewController:sender:视collapse财产,你需要创建要在细节显示控制器.
例如,在横向模式的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];
}
Run Code Online (Sandbox Code Playgroud)
我希望这能解决你的问题.
| 归档时间: |
|
| 查看次数: |
8619 次 |
| 最近记录: |