self.navigationController 在 swift 中为零 [英] self.navigationController is nil in swift
问题描述
我的故事板中有 6 到 7 个视图控制器,在这个视图控制器中,我将 self.navigationController 设为 nil,但在其他任何地方都可以使用.我在其他地方都做过同样的事情,但这个不起作用.我想不通为什么?导入 UIKit
I have 6 to 7 viewControllers in my storyBoard and in this view controller I get self.navigationController as nil but works every where else . I've done the same thing every other place but this one doesn't work. I couldn't figure out why? import 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.
The easiest way to fix this is to use a segue.
您说您想在实际进入下一个屏幕之前检查按下下一个按钮时的条件.
You said you wanted to check on a condition when the next button is pressed before actually going to the next screen though.
因此,您首先需要添加转场.但不是从 Next 按钮将它添加到视图控制器,您需要将它从第一个视图控制器(小黄框)添加到第二个视图控制器.
So, you first need to add the segue. But instead of adding it from the Next button to the view controller you need to add it from the first view controller (little yellow box) to the second view controller.
像这样...
然后给 segue 一个标识符,如 WalkThrough1Segue
.
Then give the segue an identifier like WalkThrough1Segue
.
现在,如果您的代码将具有像这样的下一个按钮功能...
Now if your code you will have your next button function like this...
func navigateTowkScreen3() {
// first check your condition
if (/*some condition*/) {
// trigger the segue
performSegueWithIdentifier("WalkThrough1Segue")
}
}
这篇关于self.navigationController 在 swift 中为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!