代码之家  ›  专栏  ›  技术社区  ›  Chandler Long

根据选择的选项,为视图控制器呈现不同的视图:Swift 4

  •  0
  • Chandler Long  · 技术社区  · 6 年前

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

    例如,如果在菜单栏上选择“设置”选项。我会被带到 选择控制器 并让selectionController继承名为

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

    家庭控制器

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

    选择一个选项(以索引路径的形式)后,以及菜单动画完成后。我在 设置新的控制器。

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

    2 回复  |  直到 6 年前
        1
  •  0
  •   u84six    6 年前

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

    //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()
    }
    
        2
  •  0
  •   Tony Lin    6 年前

    我个人认为 ViewController 是个问题。它给了你一些额外的好处,比如生命周期控制,你可以利用这个视图。

    一个很好的解释: https://stackoverflow.com/a/30459226/7591205

    视图控制器 .

    但如果你坚持要 View 为1 视图控制器