导航回iOS中的特定屏幕时出现问题

时间:2020-03-02 08:49:55

标签: swift navigation storyboard

我在调用多个屏幕并返回到应用界面中的特定屏幕时遇到了一些复杂的情况。我有一个homeVC,当用户单击homeVC时,它嵌入在导航控制器中,它显示另一个屏幕,称为detailVC。这是呼叫屏幕的层次结构 enter image description here

在LastVC中提交一个api后,我想直接导航到DetailVC屏幕,我尝试了多种方法,但是我的应用程序卡住了。我已经尝试过,

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

通过此代码,我导航到HomeVC,但也会卡住,

<!doctype html>
<html lang = "English">
    <head>
        <meta charset="UTF-8">
        <title>Array Sum</title>
        <?php 
            $x = 0; 
            $nums[5] = [0, 0, 0, 0, 0];
            $sum = 0;
        ?>
    </head>
    <body>
        <form action="" method="post">
                    <p>
                        Number: <input type = "number" name = "number" size = "40"/> <?php echo " (Number: ", $x+1, ")"; ?>
                    </p>
                    <p>
                        <input type = "submit" name = "send" value = "Send"/>
                        <input type = "reset" name = "cancel" value = "Cancel"/>
                    </p>
                </form>
        <?php 

            echo "-------->DEBUG<--------<br>";
            echo "Var X is set: ", (isset($x)) ? "true" : "false", "<br>";
            echo "Var NUMS is seta: ", (isset($nums)) ? "true" : "false", "<br>";
            echo "Var SUM is set: ", (isset($sum)) ? "true" : "false", "<br>";
            echo "----->END DEBUG<-----<br>";

            if ($x < 5) {
                if (isset($_POST["number"])) {
                        $nums[$x] = $_POST["number"];
                        $x +=1;
                }
            }

            if ($x == 5) {
                for ($index = 0; $index<count($nums); $index++)
                    $sum += $nums[$index];

                echo "<center><b>The sum of the array is : $sum </b></center>";
            }
        ?>
    </body>
</html>

主要问题是应用卡住,并且其中没有任何操作。我如何做到这一点,我尝试了许多解决方案,但未能完成。

1 个答案:

答案 0 :(得分:1)

当我检查您的应用程序流程时,然后出现两个视图控制器,即主页显示详细视图和详细视图显示vc。

但是当您尝试在任何已经显示的viewController上显示另一个视图时,可能会出错。

让我为此提供一些逻辑。了解以下代码并在您的应用中使用。也许会对您有帮助。

HomeViewController:

expect

DetailVC:

当您介绍explainVC时,请使用以下代码

Override func ViewDidLoad()
{


    //Add Obeserver for presenting ExplainVC.

    NotificationCenter.default.addObserver(self,selector: #selector(PresentExplainVC),name: NSNotification.Name(rawValue: “presentexplainvc”),object: nil)


    //Add Obeserver for presenting DetailVC.

     NotificationCenter.default.addObserver(self,selector: #selector(PresentDetailVC),name: NSNotification.Name(rawValue: “presentdetailvc”),object: nil)

}


@objc func PresentExplainVC()
{
 //write code for presenting your ExplainVC
}

@objc func PresentDetailVC()
{
 //write code for presenting your DetailVC
}

ExplainVC:

self.navigationController?.dismiss(animated: true, completion: {
 NotificationCenter.default.post(name: Notification.Name("presentexplainvc"), object: nil)
        })

VC1 :(无论其列表或形式如何)

Override func ViewDidLoad()
{

 NotificationCenter.default.addObserver(self,selector: #selector(PresentDetailVCFromHome),name: NSNotification.Name(rawValue: “presentdetailvcfromhome”),object: nil)

}


@objc func PresentDetailVCFromHome()
{
 self.navigationController?.dismiss(animated: true, completion: {
 NotificationCenter.default.post(name: Notification.Name("presentdetailvc"), object: nil)
        })
}

VC2 :(无论其列表或形式如何)

Override func ViewDidLoad()
{

 NotificationCenter.default.addObserver(self,selector: #selector(PopviewtoexplainVC),name: NSNotification.Name(rawValue: “popviewtoexplainvc”),object: nil)

}


@objc func PopviewtoexplainVC()
{


DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("presentdetailvcfromhome"), object: nil)
        })
self.navigationController?.popViewController(animated: false)
}

LastVC:

使用以下代码将其弹出到DetailVC

Override func ViewDidLoad()
{

 NotificationCenter.default.addObserver(self,selector: #selector(PopviewtoVC1),name: NSNotification.Name(rawValue: “popviewtovc1”),object: nil)

}


@objc func PopviewtoVC1()
{


DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
NotificationCenter.default.post(name: Notification.Name("popviewtoexplainvc"), object: nil)
     })   
self.navigationController?.popViewController(animated: false)
}