Search code examples
swiftif-statementuinavigationcontrollercell

If i use multiple "if statements" then hitting error (Terminating app due to uncaught exception) in swift


I am using PopoverView to show options in tableview cell button like this screen shot

here if i use like this no error.. but if check conditions with if statements then crash and error

 @objc func showOptions(sender:UIButton) {
   
    popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
 }

but here for each cell i need to show different options according to below conditions

code for cell button action: here open, close, in progress.. all are segmented modes

    @objc func showOptions(sender:UIButton) {
   
    if mode == .Open{
        popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
    }
    if (mode == .Open) && (bidsCount > 0){
        popoverView?.show(with: ["View", "View Proposal", "Edit", "Delete"], sender: sender, showDirection: .up)
    }
    if mode == .In_progress{
        popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
    }
    if mode == .Awarded{
        popoverView?.show(with: ["View", "Delete"], sender: sender, showDirection: .up)
    }
    if mode == .Closed{
        popoverView?.show(with: ["View"], sender: sender, showDirection: .up)
    }
}

PopoverView code:

extension PageContentViewController: PopoverViewDelegate {
func value(didSelect item: String, at index: Int, indexPath: IndexPath, sender: UIView) {
    if item == "View" {
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC1") as? VC1
        self.navigationController?.pushViewController(vc!, animated: true)
    }
    if item == "View Proposal" {
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC2") as? VC2
        self.navigationController?.pushViewController(vc!, animated: true)
    }
     if item == "Post Review" {
        let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "VC3") as? VC3
        self.navigationController?.pushViewController(vc!, animated: true)
    }
}

error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally a view controller <UITableViewController: 0x7fdb37092420> that is already being presented by <TestApp.SideMenuController: 0x7fdb36659370>.'


Solution

  • if mode == .Open{
        popoverView?.show(with: ["View", "View Proposal", "Delete"], sender: sender, showDirection: .up)
    }
    if (mode == .Open) && (bidsCount > 0){
        popoverView?.show(with: ["View", "View Proposal", "Edit", "Delete"], sender: sender, showDirection: .up)
    }
    

    This presents a popover, and then presents a second popover, which is not allowed. Any time (mode == .Open) && (bidsCount > 0) is true, mode == .Open is also true.

    You probably meant to reorder these and to use else if rather than just if.

    Generally, however, this would be done with a switch statement, such as:

    switch mode {
        case .Open where bidsCount > 0:
            ...
        case .Open:
            ...
        case .In_progress:
            ...
        ...
    }
    

    (Note that enum cases are generally camelcase in Swift. So these would traditionally be .open and .inProgress.)