我有一个主视图 Controller 、一个容器 View 和 2 个 subview Controller ,我希望能够在 subview Controller 之间切换(例如:当应用程序第一次加载时,我希望 Controller 包含要加载的 MapView,当我按下主视图中的搜索栏时,将加载带有表格的 Controller )。 这是我的 Storyboard:/image/rDPMe.png
主屏幕.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还有更多,但到目前为止还没有运气。我被困在这里,不知道如何解决这个问题。
堆栈:/image/Zqpm1.png SIGABR 1 错误:/image/NBgEN.png
最佳答案
您似乎正在尝试在 subview Controller 之间手动转换,但同时使用 segues(它们会为您进行自己的转换)。消除转场(初始嵌入转场除外,如果您使用的是带有“容器 View ”的 Storyboard),只需使用 Storyboard ID 手动实例化 subview Controller 。但是不要使用 segues 然后尝试替换 prepare(for:sender:)
中的 subview Controller 。
此外,当您使用 transition(from:to:duration:options:animations:completion:)
时,您不应该自己将 View 添加到 View 层次结构中。该方法会为您完成此操作(除非您使用 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()
}
}
产生:
关于ios - 无法切换到容器 View 中的另一个 subview Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53818544/