iOS中从右到左的NavigationController

时间:2012-08-18 19:39:23

标签: iphone ios uitableview uiviewcontroller

我正在建立一个从右到左的导航控制器来支持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。

任何想法?

2 个答案:

答案 0 :(得分:4)

考虑到你实际上是在攻击导航控制器,为了保持行为的一致性,你需要更多地破解它。

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

我会假设可变数组存在于您可以随时访问它的某个地方,为了简单起见,将其称为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];

免责声明:此处的代码未经测试且未经测试,我甚至不确定这是否可行,但它应该让您继续前进。祝你好运!

答案 1 :(得分:0)

受到@locke解决方案的启发,我重写了以下代码:

1-在Appdelegate中定义一个名为preViewController的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];
}