2017-08-09 38 views
-2

我想从名为“HomeController”的初始ViewController导航到名为“CalendarController”的另一个视图Controller。我想通过使用一个名为“CalendarButton”的按钮来做到这一点。我也想这样做,不参考故事板。由于如何使用按钮从一个ViewController导航到另一个ViewController。 Swift 3(No StoryBoard)

错误说:推导航控制器,不支持

import UIKit 
class HomeController: UICollectionViewController,UICollectionViewDelegateFlowLayout { 

    private let cellId = "cellId" 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    collectionView?.backgroundColor = UIColor.rgb(31, green: 194, blue: 31) 
    collectionView!.isScrollEnabled = false 
    collectionView? .register(MenuCell.self, forCellWithReuseIdentifier: cellId) 
    } 

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for:indexPath) as! MenuCell 
     cell.calendarButton.addTarget(self, action: #selector(calendar), for: .touchUpInside) 
    return cell 
    } 

    func calendar() { 
     let objVC: CalendarController! = CalendarController() 
     let aObjNavi = UINavigationController(rootViewController: objVC!) 
     self.navigationController?.pushViewController(aObjNavi, animated: true) 

    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) ->Int { 
    return 1 

    } 

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

    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 
    return 0 
    } 

} 

//单独的快捷文件

class BaseCell: UICollectionViewCell{ 
    override init(frame: CGRect) { 
    super.init(frame: frame) 
    setupViews() 
    } 
    func setupViews() { 

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

} 
    class MenuCell: BaseCell{ 

    lazy var calendarButton: UIButton = { 
    let button = UIButton(type: .system) 
    button.setTitle("Calender", for: .normal) 
    button.setTitleColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1), for: .normal) 
    button.backgroundColor = .white 
    button.titleLabel!.font = UIFont.systemFont(ofSize: 20) 
    button.layer.cornerRadius = 15 
    return button 
}() 

    func calendar() { 
    let newViewController = CalendarController() 
    self.navigationController?.pushViewController(newViewController, animated: true) 
} 
+0

该应用程序代表不是一个视图控制器 – matt

+0

显示如下代码:class HomeController ... –

+0

希望此编辑帮助 –

回答

0

因为我已经看到你的代码中没有嵌入导航控制器。 推动工作在一个堆栈中。所以你需要以编程方式在你的代码中嵌入导航控制器。

此链接可以帮助您更好地: Embedding navigation controller

+0

我更新了我的代码,现在即时获取“推送导航控制器不受支持”错误。请帮助我是新手 –

1

细胞不具有导航控制器。只有视图控制器有导航控制器。

如果您要访问

func calendar() { 
    let newViewController = CalendarController() 
    self.navigationController?.pushViewController(newViewController, animated: true) 
} 

然后改变一些像这样的代码中的HomeController

,并确保您的HomeController在内嵌NavigationController

或者您可以使用代表访问func Calender(){...}

+0

我对HomeController进行了更改,并知道我收到一条错误消息,指出不支持导航控制器。接下来我需要做什么? –

+0

当你实例化你的HomeController时,它必须被嵌入到** NavigationViewController **中。 –

相关问题