无法在容器视图中切换到另一个子视图控制器

时间:2018-12-17 15:45:53

标签: ios swift uitableview mkmapview

我有一个主视图控制器,一个容器视图和2个子视图控制器,我希望能够在子视图之间切换(例如:当应用程序首次加载时,我希望该控制器包含要加载的MapView,当我按主视图中的搜索栏时,即带有要加载的表的控制器)。 这是我的故事板:https://i.stack.imgur.com/rDPMe.png

MainScreen.swift

class MainScreen: UIViewController {

@IBOutlet private weak var searchBar: UISearchBar!
@IBOutlet private weak var ContainerView: UIView!
//private var openSearchBar: Bool?
private var openMapView: Bool = true
private var openPlacesList: Bool = false
private var containerView: ContainerViewController!

override func viewDidLoad() {
    super.viewDidLoad()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    //let containerView = segue.destination as? ContainerViewController
    if containerView == nil{
        containerView = segue.destination as? ContainerViewController
    }
    if openMapView == true{
        containerView!.moveToMapView()
    }
    else if openPlacesList == true{
        containerView!.MoveToOpenPlaces()
    }
  }
}
//search bar delegate functions
extension MainScreen: UISearchBarDelegate{
//detects when text is entered
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    openPlacesList = true
    openMapView = false
    containerView!.MoveToOpenPlaces()
  }
}

ContainerViewController.swift:

class ContainerViewController: UIViewController {

private var childViewController: UIViewController!

private var first: UIViewController?
private var sec: UIViewController?

override func viewDidLoad() {
    super.viewDidLoad()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "MainToMap"{

        first = segue.destination as! MapViewController
        self.addChild(first!)
        self.view.addSubview(first!.view)
        self.didMove(toParent: self)
    }else{
       sec = segue.destination as! PlacesListController
    }

    if(first != nil && sec != nil){
        interchange(first!,sec!)
    }
}

func interchange(_ oldVc: UIViewController,_ newVc: UIViewController ){
    oldVc.willMove(toParent: nil)
    self.addChild(newVc)
    self.view.addSubview(newVc.view)

    self.transition(from: oldVc, to: newVc, duration: 2, options: UIView.AnimationOptions.transitionCrossDissolve, animations: {
        newVc.view.alpha = 1
        oldVc.view.alpha = 0
    }, completion: { (complete) in
        oldVc.view.removeFromSuperview()
        oldVc.removeFromParent()
        newVc.willMove(toParent: self)
    })
}

func moveToMapView(){
    performSegue(withIdentifier: "MainToMap", sender: nil)
}

func MoveToOpenPlaces(){
    performSegue(withIdentifier: "MainToSearches", sender: nil)
}

}

问题是当我按下搜索栏时,它调用方法互换,然后仅给出SIGABRT 1错误。我尝试了本教程:https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html#//apple_ref/doc/uid/TP40007457-CH11-SW1,还有更多内容,但到目前为止还没有运气。我被困在这里,不知道如何解决这个问题。

堆栈:https://i.stack.imgur.com/Zqpm1.png SIGABR 1错误:https://i.stack.imgur.com/NBgEN.png

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试在子视图控制器之间进行手动转换,但同时使用segues(它们会为您自己进行转换)。消除序列(如果您使用的是带有“容器视图”的情节提要,则不包括最初的嵌入情节),只需使用其情节提要ID手动实例化子视图控制器。但是请不要使用segues,然后尝试替换prepare(for:sender:)中的子视图控制器。

此外,当您使用transition(from:to:duration:options:animations:completion:)时,不应自己将视图添加到视图层次结构中。该方法为您做到了(除非您使用showHideTransitionViews选项,否则将告诉您您正在接管此方法,而在此无需执行此操作)。同样,当您使用transitionCrossDissolve选项时,也不需要弄乱alpha。


因此,使用您所引用的文章中的代码段,您可以执行以下操作:

class FirstViewController: UIViewController {

    @IBOutlet weak var containerView: UIView!  // the view for the storyboard's "container view"
    @IBOutlet weak var redButton: UIButton!    // a button to transition to the "red" child view controller
    @IBOutlet weak var blueButton: UIButton!   // a button to transition to the "blue" child view controller

    // tapped on "transition to red child view controller" button

    @IBAction func didTapRedButton(_ sender: UIButton) {
        redButton.isEnabled = false
        blueButton.isEnabled = true

        let oldVC = children.first!
        let newVC = storyboard!.instantiateViewController(withIdentifier: "RedStoryboardID")
        cycle(from: oldVC, to: newVC)
    }

    // tapped on "transition to blue child view controller" button

    @IBAction func didTapBlueButton(_ sender: UIButton) {
        blueButton.isEnabled = false
        redButton.isEnabled = true

        let oldVC = children.first!
        let newVC = storyboard!.instantiateViewController(withIdentifier: "BlueStoryboardID")
        cycle(from: oldVC, to: newVC)
    }

    func cycle(from oldVC: UIViewController, to newVC: UIViewController) {
        // Prepare the two view controllers for the change.
        oldVC.willMove(toParent: nil)
        addChild(newVC)

        // Get the final frame of the new view controller.
        newVC.view.frame = containerView.bounds

        // Queue up the transition animation.
        transition(from: oldVC, to: newVC, duration: 0.25, options: .transitionCrossDissolve, animations: {
            // this is intentionally blank; transitionCrossDissolve will do the work for us
        }, completion: { finished in
            oldVC.removeFromParent()
            newVC.didMove(toParent: self)
        })
    }

    func display(_ child: UIViewController) {
        addChild(child)
        child.view.frame = containerView.bounds
        containerView.addSubview(child.view)
        child.didMove(toParent: self)
    }

    func hide(_ child: UIViewController) {
        child.willMove(toParent: nil)
        child.view.removeFromSuperview()
        child.removeFromParent()
    }

}

结果是:

enter image description here

相关问题