简体   繁体   中英

Dismiss one custom popup UIViewController and present another custom popup UIViewController straight away -SWIFT

I have an application with a main UIViewController. When I press a button ("Save") I have a custom popup (UIViewController) present itself. Within this popup I have another button, whereby if I press it I want to dismiss the current popup view controller and then immediately present another different custom popup view controller. I am able to dismiss the first popup , but then i get an error(see below). I am using a protocol to get this working, but I am making a mistake somewhere. Please can some one advise?

[![enter image description here][1]][1]

class mainViewController: UIViewController, popUpDismissedDelegate{

  // CUSTOM PROTOCOL DELEGATE FUNCTION
  func popUpDimissed() {

    // SHOW ANOTHER POPUP TO CREATE CUSTOM HASHTAGS!
    let createTagVC = storyboard?.instantiateViewController(withIdentifier: "createTag") as! CreateHashTagPopUpViewController
    present(createTagVC, animated: true, completion: nil)
 }


 // SAVE PDF
 @IBAction func savePdf(_ sender: Any) {

    // SHOW CUSTOM SELECTION OH HASHTAGS TO ASSIGN PDF
    let popUpVC = storyboard?.instantiateViewController(withIdentifier: "hashtagpicker") as! CustomHashTagPopup
    popUpVC.delegate = self
    present(popUpVC, animated: true, completion: nil)
 }

}



protocol popUpDismissedDelegate {
   func popUpDimissed()
}

class CustomHashTagPopup: UIViewController, UITableViewDelegate, UITableViewDataSource{

  var delegate: popUpDismissedDelegate!

 // TAP ON CELLS
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    if(indexPath == [0,0]){
        // OPTION TO CREATE A NEW HASHTAG
        self.dismiss(animated: true) {
            self.delegate.popUpDimissed()
        }

    }else{
        // DO NOTHING
        // TO SELECT A HASH TAG USE NEEDS TO PRESS ON THE CHECKBOX!
    }
}

}

ERROR:

 Warning once only: Detected a case where constraints ambiguously 
 suggest a height of zero for a tableview cell's content view. We're 
 considering the collapse unintentional and using standard height 
 instead.
 popup - viewDidDisappear
 Could not cast value of type 'UIViewController' (0x10c1ec1f0) to 
 'zapdocuments.CreateHashTagPopUpViewController' (0x107cd0520).
 2018-08-28 18:18:48.196815+0100 zapdocuments[28989:4660894] Could not 
 cast value of type 'UIViewController' (0x10c1ec1f0) to 
 'zapdocuments.CreateHashTagPopUpViewController' (0x107cd0520).

You didn't actually post the error, but I think I know what the problem is. You're probably trying to present the next VC before the old one actually dismissed.

You should use the completion parameter of the dismiss method and put your delegate callback in there, to ensure you don't try to do anything until it's fully dismissed.

Move your presentation of createTagVC to the next loop of the runloop. This should guarantee that the UI is in the correct "non-presenting" state.

DispatchQueue.main.async {
    present(createTagVC, animated: true, completion: nil)
}

Sometimes, some things aren't really done until the runloop ends. Best to start fresh in the next one.

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