简体   繁体   中英

unwinding and popviewcontroller with tab bar controller

I have a cancel and save button where i enter in a item and it unwinds to the last controller when those buttons are clicked. However, when I add a tab bar controller those buttons no longer work. Can someone explain to me why this is and how to fix it? Thanks. Enclosed is the code.

@IBAction func cancel2(_ sender: UIButton) {
    // Depending on style of presentation (modal or push presentation), this view controller
    // needs to be dismissed in two different ways.
    let isPresentingInAddMealMode = presentingViewController is UINavigationController

    if isPresentingInAddMealMode {
        dismiss(animated: true, completion: nil)
    }
    else {
        navigationController!.popViewController(animated: true)
    }
}

//This method lets you configure a view controller before it's presented.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    // Check if object referenced by saveButton is the same object instance as sender.
    if saveButton2 === sender as? UIButton {
        let name = nameTextField.text ?? ""
        let photo = photoImageView.image
        //let rating = ratingControl.rating

        // Set the meal to be passed to MealTableViewController after the unwind segue.
        meal = Meal(name: name, photo: photo)
    }
}

@IBAction func unwindToMealList(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {
        if let selectedIndexPath = tableView.indexPathForSelectedRow {
            // Update an existing meal.
            meals[selectedIndexPath.row] = meal
            tableView.reloadRows(at: [selectedIndexPath], with: .none)
        }
        else {
            // Add a new meal.
            let newIndexPath = IndexPath(row: meals.count, section: 0)
            meals.append(meal)
            tableView.insertRows(at: [newIndexPath], with: .bottom)
        }
        // Save the meals.
        saveMeals()
    }
}

What do you mean by "I add a tab bar controller"? It's likely that with that in the stack (not sure where you're putting it), the presentingViewController is UINavigationController logic no longer holds, as it's now a UITabBarController. I'd ensure that presentingViewController is what you're expecting it to be.

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