简体   繁体   中英

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 . 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

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. how to can show the all information after dismissing the custom view controller.

You are almost done. present ScanAndPay from FirstView instead of CustomAlertview .

I have done using Delegate as I think it will be fit in this scenario. Follow below steps -

Step 1:

In FirstViewController ,

Add - CustomAlertviewDelegate something like this -

  class FirstViewController: UIViewController, CustomAlertviewDelegate {

Now replace your backButtonClicked method -

  @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 -

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:

Now time for 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 -

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

}

Hope it will help you to achieve what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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