2016-09-30 13 views
0

我有一个取消和保存按钮,我在一个项目中输入,当它们点击这些按钮时它展开到最后一个控制器。但是,当我添加一个标签栏控制器这些按钮不再工作。有人可以向我解释为什么这是,以及如何解决它?谢谢。封闭的代码。展开和popviewcontroller与标签栏控制器

@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() 
    } 
} 

回答

1

你是什么意思“我添加一个标签栏控制器”?有可能在堆栈中(不知道你在哪里放置它),presentingViewController is UINavigationController逻辑不再成立,因为它现在是一个UITabBarController。我会确保presentingViewController是你所期望的。

+0

非常感谢! :3 –