2017-08-09 83 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 **中。 –

相關問題