如何在弹出窗口打开时打开视图控制器 - 快速3

时间:2017-06-05 17:52:26

标签: ios swift xcode swift3

我正在使用PopupDialog库并有一个按钮,当点按时会加载弹出窗口,如下面的代码所示:

// Create the dialog
let popup_around_me = AroundMePopUpViewController(nibName: "popup_around_me", bundle: nil)
popup_around_me.gh = self.getJobByLatitude(latitude: marker.position.latitude)
let popup = PopupDialog(viewController: popup_around_me, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)

present(popup, animated: true, completion: nil)
//print("marker.position.latitude: \(marker.position.latitude)")

在我的弹出窗口中,有两个按钮可以打开控制器。

因为我正在使用导航栏,我想我应该首先关闭弹出窗口,然后打开我的控制器。没有这样做我的导航栏没有加载。

但我不知道这是否是正确的做法。 如果没问题,我该怎么办(关闭弹出窗口并打开控制器)?

我的按钮功能:

 func cliclOnContent(tapGestureRecognizer: UITapGestureRecognizer)
    {
        print("job id: \(self.gh.id)")

//below codes not load my navigation.
//        let vc = self.storyboard!.instantiateViewController(withIdentifier: "ContentAJobViewController") as! ContentAJobViewController
//        vc.job_id = self.gh.id
//        self.navigationController?.pushViewController(vc, animated: true)

       // let storyboard = UIStoryboard(name: "Main", bundle: nil)
//        let controller = storyboard?.instantiateViewController(withIdentifier: "ContentAJobViewController") as! ContentAJobViewController
//        controller.job_id = self.gh.id
//        self.present(controller, animated: true, completion: nil)

    }

2 个答案:

答案 0 :(得分:0)

<强>编辑:

实际上你不必关闭它。它会自动关闭,您只需在相应的按钮操作中调用View控制器即可。

我有一个视图控制器,我在取消按钮中使用这样的segue打电话。

enter image description here

我的输出是这样的。我只是使用他们的演示文件来快速响应。

enter image description here

答案 1 :(得分:0)

我不熟悉上面提到的PopupDialog的生命周期,但如果它像UIAlertController那样工作,当其中一个按钮操作完成时,它会自动解除。

您似乎正在使用自定义PopupDialog版本。您应该实现与此类似的按钮和按钮操作:

    func showCustomDialog(animated: Bool = true) {

        // Create a custom view controller
        let popup_around_me = AroundMePopUpViewController(nibName: "popup_around_me", bundle: nil)


        let popup_around_me = AroundMePopUpViewController(nibName: "popup_around_me", bundle: nil)
        popup_around_me.gh = self.getJobByLatitude(latitude: marker.position.latitude)
        // Create the dialog
        let popup = PopupDialog(viewController: popup_around_me, buttonAlignment: .horizontal, transitionStyle: .bounceDown, gestureDismissal: true)


        // Create first button
        let buttonOne = CancelButton(title: "CANCEL", height: 60) {
            // Do what you want when you cancel
        }

        // Create second button
        let buttonTwo = DefaultButton(title: "Open VC", height: 60) {
            self.gotoMyOtherViewController()
        }

        // Add buttons to dialog
        popup.addButtons([buttonOne, buttonTwo])

        // Present dialog
        present(popup, animated: true, completion: nil)
    }
}   

注意在“Open VC”按钮操作中对self.gotoMyOtherViewController()的调用,这是您打开另一个VC的地方,因为您使用的是navigation bar而不是segue

func gotoMyOtherViewController(){
    let vc = self.storyboard!.instantiateViewController(withIdentifier: "ContentAJobViewController") as! ContentAJobViewController
    vc.job_id = self.gh.id        
    navigationController?.pushViewController(vc, animated: true)
}