【问题标题】:Sencha Touch Ext.navigation.View pop to rootSencha Touch Ext.navigation.View pop to root
【发布时间】:2012-01-09 14:46:09
【问题描述】:

我有一个 Ext.navigation.View,我在其中推送了一些视图。某些用户交互要求我直接返回导航视图的顶层——相当于 iOS 中 UINavigationController 上的 popToRootViewControllerAnimated:。

我尝试过各种方法,例如:

while(navigationView.getItems().getCount() > 1)
    navigationView.pop();

while(navigationView.canPop())
    navigationView.pop();

都不行。第一个例子似乎让我陷入了无限循环,这并不奇怪。第二个例子似乎只弹出一个视图。

所以问题是:在 Sencha Touch(版本 2 开发者预览版)中的 Ext.navigation.View 中弹出到根视图的正确方法是什么?

【问题讨论】:

    标签: javascript navigation sencha-touch-2


    【解决方案1】:

    有许多临时方法可以实现这一目标。

    我使用的是弹出一个比你曾经拥有的级别数更高的数字,例如

    navigationView.pop(10);
    

    效果很好,但我对此并不满意,但我看到他们现在引入了一种重置方法。你可以这样称呼它......

    navigationView.reset();
    

    在 Sencha 源代码内部(见下文),您可以看到它与 @Mithralas 所说的工作类似,但更容易编写。

    // From the Sencha source code
    this.pop(this.getInnerItems().length);
    

    【讨论】:

      【解决方案2】:
      popToRoot: function() {
           this.pop(this.getItems().length - 1);
      }
      

      【讨论】:

      • 在我写我的问题的时候,这段代码在最新的开发者预览版中是行不通的。由于现在这是正确的方法,因此我正在更改我接受的答案。
      • 从 2.0 开始,正确的做法是 navigationView.reset()
      【解决方案3】:

      确保使用 NavigationView.reset() 方法。为了清楚起见,如果您的主导航视图是 Main,您将在控制器中执行以下操作:

      this.getMain().reset();

      【讨论】:

        【解决方案4】:

        解决方案原来是使用以下内容扩展导航视图:

        popToRoot: function(destroy)
        {
            var navBar = this.getNavigationBar(),
            stackLn = this.stack.length,
            stackRm;
        
            //just return if we're at root
            if(stackLn <= 1) return;
            //just return if we're already animating
            if(navBar && navBar.animating) return;
        
            //splice the stack to get rid of items between top and root
            stackRm = this.stack.splice(1, stackLn-2);
            //remove views that were removed from the stack if required
            if(destroy) {
                stackRm.forEach(function(val, idx, arr) {
                    this.remove(val, true);
                });
            }
            //clear out back button stack
            navBar.backButtonStack = [];
            //now we can do a normal pop
            this.pop();
        }
        

        【讨论】:

        • 我在 Sencha 的论坛上发布了这个问题,得到的答案是有一种方法可以将活动项目设置为 root,但它不会修改视图堆栈。正是在这一点上,我决定添加自己的例程来做到这一点。我也在 Sencha 论坛上发布了我的解决方案,希望他们能将此功能添加到 Sencha Touch 2.0
        • 从 Sencha Touch 2 开始,你可以说 this.pop(this.getItems().length);
        • 是的。他们补充说,在我向他们发布我的解决方案并要求默认包含类似的功能之后。
        • 我不知道该政策,但也许您应该将此添加到答案的开头。我们不希望任何人将这段代码用于当前版本,对吧?
        • 我已将接受的答案更新为引用我理解的正确方法的答案。
        猜你喜欢
        • 2021-12-22
        • 2013-01-03
        • 1970-01-01
        • 2012-05-27
        • 2012-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多