简体   繁体   English

iOS中从右到左的NavigationController

[英]NavigationController Right to Left in iOS

I am buiding a Right to Left navigation controller to support RTL Languages. 我正在建立一个从右到左的导航控制器来支持RTL语言。 After reading some posts in StackOverFlow Push ViewController from Right To Left with UINavigationController , I decided that this method is most suitable: 在阅读StackOverFlow中的一些帖子后, 使用UINavigationController从右到左推送ViewController ,我认为这种方法最合适:

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];

This will create a detailed view controller and add it to the stack of view controllers just below the NavigationController. 这将创建一个详细的视图控制器,并将其添加到NavigationController正下方的视图控制器堆栈中。 When popping the NavigationController, it will appear as if we are actually loading the DetailedViewController, neat, eh? 当弹出NavigationController时,它看起来好像我们实际上正在加载DetailedViewController,整洁,嗯?

Now I face two problems: 现在我面临两个问题:

1- The Detailed View Controller no longer displays the return button. 1-详细视图控制器不再显示返回按钮。 So I decided to add a new button to do this on its behalf. 所以我决定添加一个新按钮来代表它。

2- I did not know how to return back to the NavigationController from the DetailedViewController. 2-我不知道如何从DetailedViewController返回到NavigationController。

Any Ideas? 有任何想法吗?

Considering that you're essentially hacking into the navigation controller, in order to keep the behavior consistent, you need to hack it some more. 考虑到你基本上是在攻击导航控制器,为了保持行为的一致性,你需要更多地破解它。

Popping a navigation controller releases it from the memory, so you would probably need another array or stack containing popped controllers (pushed controllers from the users perspective, because as far as I see it, you're popping whenever you need to push, and pushing whenever you need to pop). 弹出导航控制器会将其从内存中释放出来,因此您可能需要另一个包含弹出控制器的阵列或堆栈(从用户角度推送控制器,因为据我所知,无论何时需要推送,都会弹出每当你需要弹出)。 In that array/stack, you would keep the controllers you need to come back to, pushing them into the array/stack just before you pop them. 在该数组/堆栈中,您将保留需要返回的控制器,在弹出它们之前将它们推入阵列/堆栈。

I'll assume that the mutable array exists somewhere you can access it at all times, and call it otherNavigationController for the sake of simplicity: 我假设可变数组存在于您可以随时访问它的某个地方,为了简单起见,将其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];

As for the second part of your question, you would need to add custom back button because the default one wouldn't work for you. 至于问题的第二部分,您需要添加自定义后退按钮,因为默认值不适合您。 The custom button should on press push a view controller from the top of the aforementioned array/stack (pop it from the users perspective). 按下自定义按钮应从上述阵列/堆栈的顶部按下视图控制器(从用户角度弹出)。

The code for the pop function would go something like this: pop函数的代码将是这样的:

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

Disclaimer: The code here is untried and untested and I'm not even sure this would work, but it should get you on your way. 免责声明:此处的代码未经验证且未经测试,我甚至不确定这是否可行,但它应该让您顺利进行。 Good luck! 祝好运!

Inspired by the solutions from @locke I rewrote the code as follows: 受@locke解决方案的启发,我重写了以下代码:

1- Define a UIViewController in the appdelegate called preViewController to hold the master view controller. 1-在appdelegate中定义一个名为preViewController的UIViewController来保存主视图控制器。

2- To reference it in the view controllers use the: 2-要在视图控制器中引用它,请使用:

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

3- write the following masterviewcontroller as follows: 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 - Add a button to replace the default button and define an IBaction (backClicked) for the detailed view controller: 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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM