4

我正在构建一个从右到左的导航控制器来支持 RTL 语言。在阅读了 StackOverFlow Push ViewController from Right To Left with UINavigationController中的一些帖子后,我认为这种方法最合适:

DetailedViewController *DVC = [[DetailedViewController alloc]initWithNibName:@"DetailedViewController" bundle:nil];
NSMutableArray *vcs =  [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs insertObject:DVC atIndex:[vcs count]-1];
[self.navigationController setViewControllers:vcs animated:NO];
[self.navigationController popViewControllerAnimated:YES];

这将创建一个详细的视图控制器并将其添加到 NavigationController 下方的视图控制器堆栈中。当弹出 NavigationController 时,它会看起来好像我们实际上正在加载DetailedViewController,整洁,嗯?

现在我面临两个问题:

1- 详细视图控制器不再显示返回按钮。所以我决定添加一个新按钮来代表它执行此操作。

2- 我不知道如何从DetailedViewController 返回NavigationController。

有任何想法吗?

4

2 回答 2

5

考虑到您实际上是在入侵导航控制器,为了保持行为一致,您需要再修改它一些。

弹出导航控制器会将其从内存中释放,因此您可能需要另一个包含弹出控制器的数组或堆栈(从用户的角度来看,推送控制器,因为据我所知,您在需要推送时弹出,并推送每当你需要弹出)。在该阵列/堆栈中,您将保留需要返回的控制器,在弹出它们之前将它们推入阵列/堆栈。

我假设可变数组存在于您可以随时访问的某个地方,并otherNavigationController为简单起见调用它:

DetailedViewController *DVC = [[DetailedViewController alloc]initWithNibName:@"DetailedViewController" bundle:nil];
NSMutableArray *vcs = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
[vcs insertObject:DVC atIndex:[vcs count]-1];
[self.navigationController setViewControllers:vcs animated:NO];
[otherNavigationController addObject:self];
[self.navigationController popViewControllerAnimated:YES];

至于问题的第二部分,您需要添加自定义后退按钮,因为默认按钮不适合您。自定义按钮应该在按下时从上述数组/堆栈的顶部推送一个视图控制器(从用户的角度弹出它)。

pop 函数的代码如下所示:

UIViewController *previousViewController = [otherNavigationController lastObject];
[otherNavigationController removeLastObject];
[self.navigationController pushViewController:previousViewController animated:YES];

免责声明:这里的代码未经尝试和测试,我什至不确定这是否可行,但它应该能让你上路。祝你好运!

于 2012-08-19T01:44:27.297 回答
1

受@locke 解决方案的启发,我将代码重写如下:

1- 在名为 preViewController 的 appdelegate 中定义一个 UIViewController 来保存主视图控制器。

2-要在视图控制器中引用它,请使用:

AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];

3-编写以下masterviewcontroller如下:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //create a mutable arry to hold the view controllers and copy the current list of navigation controllers
    //at this point there is only the current view controller in stack

    NSMutableArray *vcs =  [NSMutableArray arrayWithArray:self.navigationController.viewControllers];

    //create a detailed navigation controller
    DetailedViewController *DVC = [[DetailedViewController alloc]initWithNibName:@"DetailedViewController" bundle:nil];

    //create a shared variable enviroment to reference a global variable 
    AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];

    //assign the current viewcontroller to the temporary view controller
    appdelegate.preViewController=[self.navigationController.viewControllers lastObject];

    //insert detailed view into the array vcs before the current viewcontroller
        if (![vcs containsObject:DVC]) {
        [vcs insertObject:DVC atIndex:[vcs count]-1];
     }
    // update the self.navigation with the new stack
    [self.navigationController setViewControllers:vcs animated:NO];

    // pop the  othernavigationcontroller from the navigation stack to fake a detailedview controller push 
     [self.navigationController popViewControllerAnimated:YES];
}

4 - 添加一个按钮来替换默认按钮并为详细视图控制器定义一个 IBaction (backClicked):

- (IBAction)backClicked:(id)sender{
AppDelegate *appdelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
//push the holder view controller to fake a pop back to the master view controller
[self.navigationController pushViewController:appdelegate.preViewController animated:YES];
}
于 2012-08-19T17:13:45.303 回答