user5184878
user5184878

Reputation: 101

navigationController is nil while performing segue?

I am using a library called SwiftPages. It works fine. But when i perform a Push Segue from this View Controller to another the navigation bar self.navigationController is nil ?

How can i add the navigation bar into the pushed VC ?

ViewController

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)
        }

EDIT

I have added the Navigation controller as my Initial VC. The Course page Controller is shown from the SwiftPages.

I can't find how it is being presented. Please take a look at this file. https://github.com/GabrielAlva/SwiftPages/blob/master/SwiftPages/SwiftPages.swift

Thanks in Advance!

Upvotes: 1

Views: 1950

Answers (1)

Rohit Kumar
Rohit Kumar

Reputation: 887

Did you put CoursePageController in NavigationController? If yes then check how it is presented?

If it is presented modally then you won't get the navigationController reference.

This will work if you have navController as rootViewController

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

Upvotes: 2

Related Questions