Ionic 3:使用手机的后退按钮关闭模式 [英] Ionic 3 : Close modal with phone's back button

查看:22
本文介绍了Ionic 3:使用手机的后退按钮关闭模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的 Ionic 应用程序中覆盖手机的后退按钮.

I try to override the phone's back button in my Ionic App.

如果我不在页面中,此代码允许我打开一个模式来关闭应用程序,否则关闭页面.

This code permit me to open a modal to close the App if I'm not in a page, else close the page.

但这不允许我关闭打开的模式.如何检测我是否处于关闭模式?

But this doesn't allow me to close an opened modal. How can I detect if I'm in a modal to close it ?

platform.registerBackButtonAction(() => {

      let nav = app.getActiveNav();
      let activeView: ViewController = nav.getActive();
      console.log(activeView);

      if(activeView != null){
        if(nav.canGoBack()) {
          activeView.dismiss();
        } else{
          let alert = this.alertCtrl.create({
            title: this.pdataManager.translate.get("close-app"),
            message: this.pdataManager.translate.get("sure-want-leave"),
            buttons: [
              {
                text: this.pdataManager.translate.get("no"),
                handler: () => {
                  this.presentedAlert = false;
                },
                role: 'cancel',
              },
              {
                text: this.pdataManager.translate.get("yes"),
                handler: () => {
                  this.presentedAlert = false;
                  this.platform.exitApp();
                }
              }
            ]
          });
          if(!this.presentedAlert) {
            alert.present();
            this.presentedAlert = true;
          }
        }
      }
    });
  }

推荐答案

您可以为您的模态框指定页面名称,您可以从应用程序的任何位置访问它.试试这个..

You can give page name to your modal and you can access it from anywhere in app. Try this..

import { App } from 'ionic-angular';

    constructor(public app: App){

    }

        platform.registerBackButtonAction(() => {

              let nav = this.app.getActiveNav();
              let view = nav.getActive().instance.pageName;


              if (view == YOU_PAGE_NAME) {
                //You are in modal
              } else {
                //You are not in modal
              }
        });

在你的模式中

pageName = 'YOU_PAGE_NAME';

这篇关于Ionic 3:使用手机的后退按钮关闭模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆