self.navigationController在swift中为nil

时间:2014-12-10 17:00:43

标签: swift uinavigationcontroller

我的storyBoard中有6到7个viewControllers,在这个视图控制器中,我将self.navigationController作为nil但是在其他地方工作。我在其他地方做了同样的事情,但这个没有用。我无法弄清楚为什么?     导入UIKit

class wkScreen2ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {



    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        //        tableView.estimatedRowHeight = 500
        tableView.rowHeight = UITableViewAutomaticDimension
        //        tableView.scrollEnabled = true

        // Do any additional setup after loading the view.
    }




    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return 3
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
        UITableViewCell {


            if(indexPath.row == 0){
            var cell: Cell1TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell1") as Cell1TableViewCell

            return cell
            }

            if(indexPath.row == 1){
                println("2")
                var cell: Cell2TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell2") as Cell2TableViewCell

                return cell
            }

            if(indexPath.row == 2){
                println("3")
                var cell: Cell3TableViewCell = tableView.dequeueReusableCellWithIdentifier("Cell3") as Cell3TableViewCell

                return cell
            }


           return UITableViewCell()

    }
    func navigateTowkScreen3(){
        println("next2")
            let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            var wkScr3 = mainStoryboard.instantiateViewControllerWithIdentifier("wkScreen3") as wkScreen3ViewController
        println(wkScr3)
        println(self.navigationController)
            self.navigationController?.pushViewController(wkScr3, animated: true)


    }



}

https://www.dropbox.com/s/0q7whsgiq3nw14e/Screen%20Shot%202014-12-10%20at%203.39.06%20PM.png?dl=0

1 个答案:

答案 0 :(得分:1)

解决此问题的最简单方法是使用segue。

你说你想在实际进入下一个屏幕之前检查按下一个按钮的条件。

所以,首先需要添加segue。但是,不是将它从“下一步”按钮添加到视图控制器,而是需要将其从第一个视图控制器(小黄框)添加到第二个视图控制器。

喜欢这个......

enter image description here

然后给segue一个标识符WalkThrough1Segue

现在,如果你的代码你的下一个按钮功能就像这样......

func navigateTowkScreen3() {
    // first check your condition
    if (/*some condition*/) {
        // trigger the segue
        performSegueWithIdentifier("WalkThrough1Segue")
    }
}