0
votes

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.

1
Add delay between every preset new ControllerAmir Khan
@AmirKhan tq for replay I can try!User1985
let me know if you are still having any issue.Amir Khan
@AmirKhan still same issue, when I click the password button it was not loading the pinStatusLabel.text because it was on my custom view on view controllerUser1985
Are you using Delegate to pass data?Amir Khan

1 Answers

0
votes

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.