Как я могу работать с контроллером push view на swift 5? - PullRequest
0 голосов
/ 08 мая 2019

Функция Didselect в CollectionView или TableView;

let trial = TrialViewController ()
self.navigationController?.pushViewController (trial, animated: true)

Я хочу перейти на другую страницу, но она не отвечает. То же самое работает, когда я печатаю.

self.present (den, animated: true, completion: nil)

работает, когда я печатаю.

почему не работает pushViewController?

Thnx

Ответы [ 4 ]

1 голос
/ 08 мая 2019

Кажется self.navigationController? это nil

С раскадровкой:

self.navigationController?.pushViewController (trial, animated: true) 

убедитесь, что текущий виртуальный канал встроен в навигационный контроллер

enter image description here

без раскадровки:

внутри didFinishLaunchingWithOptions

self.window?.rootViewController = UINavigationController(root:ViewController())
0 голосов
/ 08 мая 2019

Я не использую раскадровку. Мой класс здесь;

     import UIKit

class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

    let cellId = "cellId"

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupViews()
        //register collectionview
        collectionView.register(AppCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.delegate = self
        collectionView.dataSource = self
    }

    let topView: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = UIColor.white
        return view
    }()

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.backgroundColor = UIColor.orange
        return cv
    }()

    lazy var button: UIButton = {
        let btn = UIButton(type: .system)
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setTitle("Buton", for: .normal)
        btn.backgroundColor = UIColor.orange
        btn.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
        return btn
    }()

    @objc func handleButton() {
        print(1212)
    }

    func setupViews() {
        view.addSubview(topView)
        topView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
        topView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
        topView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
        topView.heightAnchor.constraint(equalToConstant: 250).isActive = true

        view.addSubview(button)
        button.topAnchor.constraint(equalTo: topView.topAnchor, constant: 15).isActive = true
        button.centerXAnchor.constraint(equalTo: topView.centerXAnchor, constant: 0).isActive = true
        button.widthAnchor.constraint(equalToConstant: view.frame.width - 15).isActive = true
        button.heightAnchor.constraint(equalToConstant: 50).isActive = true

        view.addSubview(collectionView)
        collectionView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: 10).isActive = true
        collectionView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
        collectionView.widthAnchor.constraint(equalToConstant: view.frame.width).isActive = true
        collectionView.heightAnchor.constraint(equalToConstant: view.frame.height).isActive = true
        collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true

    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! AppCell

        return cell
    }

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

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let trial = Trial()
        self.navigationController?.pushViewController(trial, animated: true)
    }
}

class AppCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame:frame)
        backgroundColor = UIColor.purple
        setupViews()
    }

    lazy var button: UIButton = {
        let btn = UIButton(type: .system)
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.setTitle("Buton", for: .normal)
        btn.backgroundColor = UIColor.orange
        btn.addTarget(self, action: #selector(handleButton), for: .touchUpInside)
        return btn
    }()

    @objc func handleButton() {
        //statusShow.showSettings()

    }

    func setupViews() {
        addSubview(button)
        button.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0).isActive = true
        button.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0).isActive = true
        button.widthAnchor.constraint(equalToConstant: self.frame.width * 0.3).isActive = true
        button.heightAnchor.constraint(equalToConstant: self.frame.height * 0.2).isActive = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class Trial: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
0 голосов
/ 08 мая 2019

Ваш текущий ViewController не является RootViewController.

В AppDelegate,

установить этот код:

var  mainView = UIStoryboard(name:"Main", bundle: nil)
let viewcontroller : UIViewController = mainView.instantiateViewController(withIdentifier: "FirstScreenVC") as! FirstScreenVC
self.window!.rootViewController = viewcontroller 

Теперь запустите ваш код, Потому что теперь текущий контроллер - rootViewController.

0 голосов
/ 08 мая 2019

Вы не должны создавать экземпляр контроллера вида, подобного этому TrialViewController ()

Вместо этого используйте:

UIStoryboard(name: "SbName", bundle: nil).instantiateViewController(withIdentifier: "VcID") as? TrialViewController
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...