0

I am trying to push to a different view when a user selects a table cell and when I get down to where it would make the push, it throws an error.

The special situation here is that I am working two UIViews with tableViews under the same UIViewController. As a result, the UIView and TableDelegates are in their own class.

Any suggestions are greatly appreciated.

import Foundation
import UIKit
import CoreData

class FavoritesViewController: UIViewController {

//MARK: - UIViewController Delegates
override func viewWillAppear(animated: Bool) {}
}


//MARK: - Lenders Table View Delegates
class FavoritesLenderViewController:  UIView, UITableViewDelegate, UITableViewDataSource {

let favoritesViewController = FavoritesViewController()
let lenderDetailsViewController = LenderDetailsViewController()

var lenders = Array<Lenders>()
var lenderUserComments = Array<LenderUserComments>()
let lendersModel = LendersModel()

//MARK:  View Parameters
@IBOutlet weak var favoriteLendersTableView: UITableView!

// Equivalent to viewWillAppear for a subclass.
override func willMoveToSuperview(newSuperview: UIView?) {
    self.lenders = lendersModel.readLenderData() as Array

    // Reload data (for when the user comes back to this page from the details page.)
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.favoriteLendersTableView.reloadData()
    })
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var favoritesViewController = FavoritesViewController()
    return self.lenders.count
}



// On selecting, send variables to target class.
func tableView(tableView: UITableView!, didSelectRowAtIndexPath path: NSIndexPath!) {

    // Setup what segue path is going to be used on selecting the row as set the type as the class for the view your going to.
    let lenderDetailsViewController = self.favoritesViewController.storyboard?.instantiateViewControllerWithIdentifier("LendersDetailsView") as LenderDetailsViewController

    // Get the lenderId from the selected path.row and put the values to the variables in the target class.
    let lendersNSArray = lenders as NSArray
    lenderDetailsViewController.lenderIdPassed = lendersNSArray[path.row].valueForKey("lenderId") as String!

    //  tell the new controller to present itself
// BREAKS HERE : EXC_BAD_INSTRUCTION code=EXC_I386_INVOP
    lenderDetailsViewController.navigationController?.pushViewController(lenderDetailsViewController, animated: true)

}

....//additional table delegates
}
Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48

1 Answers1

0

You don't push the new view controller on itself. Try this:

self.navigationController?.pushViewController(lenderDetailsViewController, animated: true)
SrB
  • 353
  • 4
  • 11
  • Thanks for the quick reply Suraj, I can't use self because "FavoritesLenderViewController" doesn't have a member named "navigationController". That member seems to exist for the UIViewController, which is a separate class which I setup to use at the top of the class "FavoritesLenderViewController". Calling it such as below throws the same error. self.favoritesViewController.navigationController?.pushViewController(lenderDetailsViewController, animated: true) – Christopher Wade Cantley Jan 02 '15 at 19:27
  • Click on the first viewcontroller in interface builder and do Editor > Embed In > Navigation Controller. now you have the nacigationController property – Oliver Ni May 12 '15 at 04:35