简体   繁体   English

如何在swift中关闭第二个viewcontroller后启用带有视图的customview控制器

[英]how to enable the customview controller with view after dismiss Second viewcontroller in swift

I have two view controller imag 1.first view controller is main and second is CustomAlertviw controller main view controller is like above image, when I click continues am showing the customalertview controller, like image . 我有两个视图控制器imag 1.第一个视图控制器是主要的,第二个是CustomAlertviw控制器主视图控制器就像上面的图像,当我点击继续显示customalertview控制器,就像图像一样。 when click the enter pin button I have to dismiss view contoller and show the view controller with uiview on main view controller but tried, it's showing the view controller, there one more custom view is not showing it crashing my app 当点击输入pin按钮时我必须关闭视图控制器并在主视图控制器上显示带有uiview的视图控制器但是尝试了,它显示了视图控制器,还有一个自定义视图没有显示它崩溃我的应用程序

main view code 主视图代码

    @IBAction func backButtonClicked(_ sender: UIButton) {

    let myAlert = UIStoryboard(name: "CustomAlertview", bundle: nil).instantiateViewController(withIdentifier: "CustomAlertViewController") as? CustomAlertViewController
            myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
            myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
         self.present(myAlert!, animated: true, completion: nil)

}

Customalertview controller 
  @IBAction func enterPinButtonClick(_ sender: Any) {

       self.dismiss(animated: true, completion: nil)
        let myAlert = UIStoryboard(name: "ScanAndPay", bundle: nil).instantiateViewController(withIdentifier: "ScanAndPayDetailsEntryViewController") as? ScanAndPayDetailsEntryViewController
        myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
        myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
        self.present(myAlert!, animated: true, completion: nil)
        myAlert.valuespass()
    }


inside main view controller one function calling from the customalrerview 

func  valuespass()
    {
        DispatchQueue.main.async {
            self.authType = 1
            self.authStatus = false
            print("this is calling")
            self.pinStatusLabel.text = "Please enter a 4-digit Security PIN"
}

when I am calling this function from the custom alert view application is crashing and showing the hread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value this error. 当我从自定义警报视图应用程序调用此函数时崩溃并显示hread 1:致命错误:在解开Optional值时意外发现nil此错误。 how to can show the all information after dismissing the custom view controller. 如何在解除自定义视图控制器后显示所有信息。

You are almost done. 你差不多完成了。 present ScanAndPay from FirstView instead of CustomAlertview . FirstView呈现ScanAndPay而不是CustomAlertview

I have done using Delegate as I think it will be fit in this scenario. 我已经完成了使用Delegate因为我认为它适合这种情况。 Follow below steps - 按照以下步骤 -

Step 1: 第1步:

In FirstViewController , FirstViewController中

Add - CustomAlertviewDelegate something like this - 添加 - CustomAlertviewDelegate这样的事情 -

  class FirstViewController: UIViewController, CustomAlertviewDelegate {

Now replace your backButtonClicked method - 现在替换你的backButtonClicked方法 -

  @IBAction func backButtonClicked(_ sender: UIButton) {

        let myAlert = UIStoryboard(name: "CustomAlertview", bundle: nil).instantiateViewController(withIdentifier: "CustomAlertViewController") as? CustomAlertViewController
        myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
        myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
        myAlert.delegate = self
        self.present(myAlert!, animated: true, completion: nil)

  }

Add a new Delegate method of CustomAlertviewDelegate - 添加CustomAlertviewDelegate的新Delegate方法 -

func ScanAndPayView(){
       DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
           let myAlert = UIStoryboard(name: "ScanAndPay", bundle: nil).instantiateViewController(withIdentifier: "ScanAndPayDetailsEntryViewController") as? ScanAndPayDetailsEntryViewController
           myAlert?.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
           myAlert?.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
           self.present(myAlert!, animated: true, completion: nil)
    }
}

Step 2: 第2步:

Now time for CustomAlertview , 现在是CustomAlertview的时间,

Add protocol for delegate top of the Class - 为类的顶级添加协议 -

protocol CustomAlertviewDelegate: class {

   func ScanAndPayView()
}

class CustomAlertview: UIViewController{
  weak var delegate : CustomAlertviewDelegate!
  ...
  override func viewDidLoad() { // Rest of your code
}

Now time to dismiss current view controller and notify FirstViewController for presenting ScanAndPay view Controller - 现在是时候关闭当前视图控制器并通知FirstViewController呈现ScanAndPay view Controller -

@IBAction func enterPinButtonClick(_ sender: Any) {
   delegate.ScanAndPayView()
   self.dismiss(animated: true, completion: nil)

}

Hope it will help you to achieve what you want. 希望它能帮助你实现你想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM