Ios 根据选择的选项,使用不同的视图显示视图控制器:Swift 4

Ios 根据选择的选项,使用不同的视图显示视图控制器:Swift 4,ios,iphone,swift,uiview,viewcontroller,Ios,Iphone,Swift,Uiview,Viewcontroller,我会尽我所能解释我的问题,所以提前谢谢你 我通常通过让每个视图控制器代表不同的视图来构建具有多个视图控制器的应用程序。现在,我正在创建一个具有许多不同视图的大型应用程序。我不希望在我的应用程序中有15个以上的ViewController。我希望一个视图控制器继承不同的UIView 例如,如果在我的菜单栏上选择“设置”选项。然后,我将被带到selectionController,并让selectionController继承名为设置视图的UIView func collectionView

我会尽我所能解释我的问题,所以提前谢谢你

我通常通过让每个视图控制器代表不同的视图来构建具有多个视图控制器的应用程序。现在,我正在创建一个具有许多不同视图的大型应用程序。我不希望在我的应用程序中有15个以上的ViewController。我希望一个视图控制器继承不同的UIView

例如,如果在我的菜单栏上选择“设置”选项。然后,我将被带到selectionController,并让selectionController继承名为设置视图的UIView

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return menuOptions.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
    let menuOption = menuOptions[indexPath.row]
    cell.menuOption = menuOption
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 30)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let menuOption = self.menuOptions[indexPath.row]
    print(menuOption.name)
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.blackView.alpha = 0

        if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
            let width = window.frame.width - 150

            self.menuView.frame = CGRect(x: -width, y: 0, width: 0, height: window.frame.height)
        }

    }) { (completed: Bool) in


//THIS IS WHERE I CALL MY FUNCTION TO SET UP THE SELECTIONCONTROLLER
        self.homeController?.displayController(menuOption: menuOption)
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)

    setUpView()
    setUpCollectionView()

}
目前,我可以导航到我的设置控制器,并让导航栏代表所选的选项。但是我该如何继承设置视图呢

HomeController

class HomeController: UIViewController {
    lazy var menu: Menu = {
        let menuLauncher = Menu()
        menuLauncher.homeController = self
        return menuLauncher
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Home"
            view.backgroundColor = Color.blue
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "menu"), style: .plain, target: self, action: #selector(displayMenu))
    }

    func displayController(menuOption: MenuOption) {
        let selectionController = UIViewController()
        selectionController.navigationItem.title = menuOption.name
        selectionController.view.backgroundColor = Color.blue
        navigationController?.navigationBar.tintColor = UIColor.white
        navigationController?.pushViewController(selectionController, animated: true)
    }

    @objc func displayMenu() {
        menu.displayMenu()
    }
}
菜单视图

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return menuOptions.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! MenuCell
    let menuOption = menuOptions[indexPath.row]
    cell.menuOption = menuOption
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width, height: 30)
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let menuOption = self.menuOptions[indexPath.row]
    print(menuOption.name)
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
        self.blackView.alpha = 0

        if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
            let width = window.frame.width - 150

            self.menuView.frame = CGRect(x: -width, y: 0, width: 0, height: window.frame.height)
        }

    }) { (completed: Bool) in


//THIS IS WHERE I CALL MY FUNCTION TO SET UP THE SELECTIONCONTROLLER
        self.homeController?.displayController(menuOption: menuOption)
    }
}

override init(frame: CGRect) {
    super.init(frame: frame)
    self.collectionView.delegate = self
    self.collectionView.dataSource = self
    collectionView.register(MenuCell.self, forCellWithReuseIdentifier: cellId)

    setUpView()
    setUpCollectionView()

}
选择选项后(以索引路径的形式),以及菜单动画完成后。我在家庭控制器中调用函数displayController并设置新控制器


我如何实现视图呢?如果选择了“设置”,如何让selectionController也显示设置视图?

您可以将子视图添加到现有视图中:

//The container view can be in the vc with constraints already setup.
//Or you can use the vc's default view.
enum MyView: Int { case settingsView = 0, otherView }

//Add view
let view = UIView(frame: containerView.bounds)
view.tag = MyView.settingsView.rawValue
containerView.addSubview(view)

//Remove view
if let remove = containerView.viewWithTag(MyView.settingsView.rawValue) {
    remove.removeFromSuperview()
}

就我个人而言,我不认为拥有许多
ViewController
是一个问题。它给视图带来了一些额外的好处,比如生命周期控制,您可以利用它

一个很好的解释:

当视图变得复杂时,最好封装到
ViewController

但是如果您坚持为1
ViewController
使用多个
View
。我的建议是使用容器视图概念,然后在其中切换视图。实现这一点的一个好方法是拥有一个完整的视图,然后将所有视图都放在其中。然后根据需要隐藏/显示