我使用UINavigationController作为rootViewController并且有一些导航层次结构问题。我从show segue状态以模态方式呈现viewController,但是当我解除模态时,它将应用程序返回到viewDidAppear navigationController状态 - 这是应用程序启动状态。我认为这是因为即使我从show navigation stack中的第二个视图调用presentViewController函数,模式也由父rootViewController处理,所以当模态被解除时,它返回到UINavigationController的viewDidAppear状态。我希望模态被解除并返回到呈现模态的viewcontroller(show segue stack中的第二个屏幕)。感谢您的帮助!
// rootViewController
class MainNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = TravelersFeedVC()
let vc2 = ProfileVC()
if isLoggedIn() {
// assume user is logged in
let homeController = HomeController()
homeController.firstViewController = vc1
homeController.secondViewController = vc2
viewControllers = [homeController]
} else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}
func showLoginController() {
let loginController = LoginController()
present(loginController, animated: true, completion: {
// perhaps do something here later
})
}
}
// HomeController
class HomeController: UIViewController, FBSDKLoginButtonDelegate {
// child view controllers to put inside content view
var firstViewController: TravelersFeedVC?
var secondViewController: ProfileVC?
private var activeViewController: UIViewController? {
didSet {
removeInactiveViewController(inactiveViewController: oldValue)
updateActiveViewController()
}
}
private func removeInactiveViewController(inactiveViewController: UIViewController?) {
if let inActiveVC = inactiveViewController {
// call before removing child view controller's view from hierarchy
inActiveVC.willMove(toParentViewController: nil)
inActiveVC.view.removeFromSuperview()
// call after removing child view controller's view from hierarchy
inActiveVC.removeFromParentViewController()
}
}
private func updateActiveViewController() {
if let activeVC = activeViewController {
// call before adding child view controller's view as subview
addChildViewController(activeVC)
activeVC.view.frame = contentView.bounds
contentView.addSubview(activeVC.view)
// call before adding child view controller's view as subview
activeVC.didMove(toParentViewController: self)
}
}
// UI elements
lazy var contentView: UIView = {
let tv = UIView()
tv.backgroundColor = UIColor.purple
tv.translatesAutoresizingMaskIntoConstraints = false
tv.layer.masksToBounds = true
return tv
}()
var segmentedController: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
activeViewController = firstViewController
// checkIfUserIsLoggedIn()
view.addSubview(contentView)
setupProfileScreen()
// navigationItem.title = "Home"
let items = ["Travelers", "Me"]
segmentedController = UISegmentedControl(items: items)
navigationItem.titleView = segmentedController
segmentedController.tintColor = UIColor.black
segmentedController.selectedSegmentIndex = 0
// Add function to handle Value Changed events
segmentedController.addTarget(self, action: #selector(HomeController.segmentedValueChanged(_:)), for: .valueChanged)
}
// reference to collectionViewController
var travelersFeedVC: TravelersFeedVC!
func segmentedValueChanged(_ sender:UISegmentedControl!)
{
switch segmentedController.selectedSegmentIndex {
case 0:
activeViewController = firstViewController
case 1:
activeViewController = secondViewController
default: // Do nothing
break
}
}
// tableviewcontroller,其中点击按钮以显示viewController - 此tableViewController是节目导航堆栈中的第二个
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "+ place", style: .plain, target: self, action: #selector(handleSearchPlaces))
}
func handleSearchPlaces() {
let _: UIViewController! = self.presentingViewController as? PlacesTableVC
let vc = SearchPlacesController()
let navController = UINavigationController(rootViewController: vc)
present(navController, animated: true, completion: nil)
}
//模态viewController被解除并返回用户到tableview *而不是rootViewController应用程序加载状态
class SearchPlacesController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "back", style: .plain, target: self, action: #selector(returnToPlacesTableVC))
}
func returnToPlacesTableVC(sender: AnyObject) {
// save the presenting ViewController
let presentingViewController: UIViewController! = self.presentingViewController as? PlacesTableVC
self.dismiss(animated: true) {
// I want to go back to PlacesTableVC - NOT RootViewController with selectedIndex 0
presentingViewController?.dismiss(animated: true, completion: nil)
}
}
}