【问题标题】:self.navigationController is nil in swiftself.navigationController 在 swift 中为零
【发布时间】:2014-12-10 17:00:42
【问题描述】:

我的故事板中有 6 到 7 个视图控制器,在这个视图控制器中,我将 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

【问题讨论】:

  • 你确定它有导航控制器吗?并非所有视图控制器都这样做。
  • 如何确定它是否有导航控制器,我猜这是一个菜鸟问题,但我对这个视图控制器做了同样的事情,就像其他人做的一样
  • 好吧,故事板里面有导航控制器吗?如果是这样,进入这个控制器的 segue 类型是什么? (你能显示故事板的截图吗?)
  • 这是屏幕截图。 wkScreen2ViewController 的导航控制器为 nil

标签: swift uinavigationcontroller


【解决方案1】:

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

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

所以,您首先需要添加转场。但是,您需要将它从第一个视图控制器(小黄色框)添加到第二个视图控制器,而不是将其从 Next 按钮添加到视图控制器。

像这样……

然后给 segue 一个标识符,如WalkThrough1Segue

现在,如果您的代码将拥有像这样的下一个按钮功能...

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

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    • 2017-11-22
    相关资源
    最近更新 更多