【问题标题】:iOS Can't dismiss view controlleriOS 无法关闭视图控制器
【发布时间】:2020-04-25 03:56:15
【问题描述】:

我的应用有问题。场景很简单,成功创建帐户后,我不想关闭当前页面或导航到登录页面。我的故事板如下所示:

成功创建帐户后,我会弹出一个包含一些信息的弹出窗口,我们会向您发送验证电子邮件,在此弹出窗口之后,我想转到左起第二个页面 - 这是我的主应用程序页面(现在称为“视图控制器” )。

我试过关闭窗口,但我在那里没有效果,它只能关闭我的弹出窗口。 当我尝试重定向时,按下后退按钮时出现问题,它会导致注册页面。有一些代码:

// Create new user and send verification email

        Auth.auth().createUser(withEmail: userEmail, password: userPassword) { user, error in if error == nil && user != nil {
            self.sendVerificationMail();
            self.displayAlertMessage(alertTitle: "Success", alertMessage: "Your account created successfully. We send you a verification email.");

            // Redirect to View Controller

            } else {
            self.displayAlertMessage(alertTitle: "Unhandled error", alertMessage: "Undefined error #SignUpViewController_0002");
            }
        }

...

func displayAlertMessage(alertTitle: String, alertMessage:String, alertRedirection:String = ""){
        let alert = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: UIAlertController.Style.alert);

        let okAction = UIAlertAction(title:"Ok", style: UIAlertAction.Style.default, handler: nil);

        alert.addAction(okAction);

        self.present(alert, animated:true, completion:nil);
    }

如果我添加这个:

self.view.window!.rootViewController?.dismiss(animated: false, completion: nil)

alert之后只关闭alert,alert之前什么都不做(同dismiss)。

【问题讨论】:

  • 您需要为您的确定按钮添加操作
  • @AndresGomes 如果我添加它,我的后退按钮有问题,如果我登录,我应该无法进入注册页面,但如果我按下返回,我会重定向到注册页面
  • nop,当您按下确定按钮时,应用程序将导航到 loginviewcontroller,这很简单,检查您如何使用导航控制器推送视图控制器,当应用程序在 loginviewcontroller 中时,您需要配置后退按钮,如果没有,应用返回 registerviewcontroller
  • @AndreasGomes - self.performSegue(withIdentifier: "test", sender: self); - 我将此添加到确定按钮,按下后我在有效页面上,但我有返回按钮,如果我在注册页面上再次按下它 - 如何避免这种情况?
  • @iSheep - 不要在“确定”按钮操作中使用 performSegue。它将向导航控制器添加更多视图。从当前视图弹出到您需要的视图控制器(主视图)并在警报操作中关闭警报。

标签: ios swift xcode redirect


【解决方案1】:

好吧,您正在使用导航控制器,例如,为了“呈现”一个新的视图控制器,您需要推送它。

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("IDOFYOURVIEW") as CLASS_NAME_OFYOUR_VIEWCONTROLLER
navigationController?.pushViewController(vc, animated: true)

使用最后的代码,您可以“呈现”(推送)一个新的视图控制器

现在,如果您想在按下后退按钮时执行其他操作,请尝试使用此行

override func viewDidLoad {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true
    let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
    self.navigationItem.leftBarButtonItem = newBackButton
}

func back(sender: UIBarButtonItem) {
    //in this part you can move to other view controller, examples
    // Go back specific view in your navigation controller
    for controller in self.navigationController!.viewControllers as Array {
    if controller.isKind(of: NAMECLASS_OFYOUR_VIEWCONTROLLER.self) {
        _ =  self.navigationController!.popToViewController(controller, animated: true)
        break
       }
    }
    // Perform your custom actions
    // ...
    // Go back to the previous ViewController
    self.navigationController?.popViewControllerAnimated(true)
}

问候

【讨论】:

    【解决方案2】:

    要关闭并弹出到主视图,您可以使用警报按钮操作处理程序。

    alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: { (action) in
    
                self.dismiss(animated: true, completion: {
                    self.navigationController?.popToRootViewController(animated: true)
                })
            }))
    

    或者您可以使用以下行导航到特定视图控制器。

    for viewController in self.navigationController!.viewControllers {
                if viewController.isKind(of: <Your_Main_ViewController_Class_Name>.self) {
                    self.navigationController?.popToViewController(viewController, animated: true)
                    break
                }
            }
    

    Your_Main_ViewController_Class_Name 是您需要导航到的导航控制器堆栈中的视图控制器。 (即)主视图

    要在显示警报弹出窗口后盲目导航到主视图,您可以在显示警报时使用完成处理程序。

    self.present(alert, animated: true, completion: {
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
                    self.navigationController?.popToRootViewController(animated: true)
                }
            })
    

    【讨论】:

    • 我正在快速学习 - 不确定我需要在 下放置什么。我尝试了 ViewControll 和测试(我命名了该控制器测试的行,但它不起作用
    • 我是根据您的第一个想法制作的,无论如何知道如何导航很好,您能告诉我我应该在 下写什么吗?
    【解决方案3】:

    视图控制器类:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
    
        override func viewDidAppear(_ animated: Bool) {
            self.performSegue(withIdentifier: "notLoggedView", sender: self);
        }
    }
    

    类 id 设置为“测试”:

    “推”:

    ...
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
                let vc = storyboard.instantiateViewController(withIdentifier: "test") as ViewController
                navigationController?.pushViewController(vc, animated: true)
    ...
    

    可悲的是错误:

    'UIViewController' 不能转换为 'ViewController'

    我哪里做错了?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2012-05-19
      • 2018-12-30
      • 2011-12-31
      • 1970-01-01
      相关资源
      最近更新 更多