【问题标题】:navigationController is nil while performing segue?执行segue时navigationController为零?
【发布时间】:2015-11-18 16:50:21
【问题描述】:

我正在使用一个名为 SwiftPages 的库。它工作正常。但是当我从这个 View Controller 执行 Push Segue 到另一个时,导航栏 self.navigationController 是 nil 吗?

如何将导航栏添加到推送的 VC 中?

视图控制器

class CoursePageController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var ChapterTable: UITableView!

    @IBOutlet weak var courseDesc: UITextView!

        var CourseName : String!

        var ChapName : [String] = []
        var ChapId : [String] = []


    override func viewDidLoad() {
        super.viewDidLoad()



        self.title = CourseName
        self.courseDesc.text = CourseDescriptionC
        self.courseDesc.setContentOffset(CGPointZero, animated: true)
        HUD()
        ChapterTable.estimatedRowHeight = 120
        ChapterTable.rowHeight = UITableViewAutomaticDimension
        getData()
    }

    func HUD(){
            progress.show(style: MyStyle())
    }

    func getData(){
        Alamofire.request(.GET, "http://www.wve.com/index.php/capp/get_chapter_by_course_id/\(CourseIdinController)")
            .responseJSON { (_, _, data, _) in
                let json = JSON(data!)
                let catCount = json.count
                for index in 0...catCount-1 {
                    let cname = json[index]["CHAPTER_NAME"].string
                    let cid = json[index]["CHAPTER_ID"].string
                    self.ChapName.append(cname!)
                    self.ChapId.append(cid!)
                }
                self.ChapterTable.reloadData()
                self.progress.dismiss()
        }
    }


   func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

     func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return ChapName.count
    }


     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell : UITableViewCell = self.ChapterTable.dequeueReusableCellWithIdentifier("ChapCell") as! UITableViewCell
        cell.textLabel?.text = self.ChapName[indexPath.row]

        return cell
    }


        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //Here the navigationController is nil
self.navigationController?.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("LessonController") as! UIViewController, animated: true)
    //        performSegueWithIdentifier("ChapSegue", sender: nil)
        }

编辑

我已将导航控制器添加为我的初始 VC。课程页面控制器显示在 SwiftPages 中。

我找不到它是如何呈现的。请看一下这个文件。 https://github.com/GabrielAlva/SwiftPages/blob/master/SwiftPages/SwiftPages.swift

提前致谢!

【问题讨论】:

  • 请检查您的标识符“LessonController”

标签: ios swift uinavigationcontroller segue


【解决方案1】:

你把 CoursePageController 放在 NavigationController 里了吗?如果是,请检查它是如何呈现的?

如果它以模态方式呈现,那么您将不会获得 navigationController 参考。

如果您将 navController 作为 rootViewController,这将起作用

if let navController = UIApplication.sharedApplication().keyWindow?.rootViewController as? UINavigationController {
            //get the nav controller here and try to push your anotherViewController
        }

【讨论】:

  • 你有navigationController作为rootViewController吗?
  • 是的。我像这样使用上面的。如果让 navController = UIApplication.sharedApplication().keyWindow?.rootViewController 为? UINavigationController { //在这里获取导航控制器并尝试推送您的 anotherViewController self.navigationController!.pushViewController(self.storyboard!.instantiateViewControllerWithIdentifier("LessonController") as! UIViewController, animated: true) } 但它不起作用跨度>
  • 那你不在navgitaionController里面。
  • 问题是 SwiftPages 视图隐藏了我的导航栏。但我怎样才能把它带回来? i.imgur.com/RKJITrD.png
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多