Anmol Rajpal Anmol Rajpal - 4 months ago 14
Swift Question

Unable to pass data from ViewControllerClass to TableViewControllerClass

i want to pass textField data from my UIViewController Class to a UITableViewController Class with the method -
which is from a button (IBAction) function
image description here

but its not working, gives an error ---

*** Terminating app due to uncaught exception


'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'attention'' //and so on


i have all stuff properly initialized..
This is my code for ViewController class ->

class CredentialViewController: UIViewController {

@IBOutlet weak var idTextField: UITextField!

@IBAction func credit(sender: AnyObject) {

if idTextField.text?.isEmpty != true {
let attendance = self.storyboard?.instantiateViewControllerWithIdentifier("attention") as! attendenceTableViewController
attendance.creden = Int(idTextField.text!)
self.navigationController?.pushViewController(attendance, animated: true)
}
else {
print("Empty")
}
}


And code for TableViewController is --->

class attendenceTableViewController: UITableViewController {
var checkins = [attendance]()
var creden:Int?

override func viewDidLoad()
{
super.viewDidLoad()
jsonParsingFromFile()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
print(creden)
}

Answer

As your ViewController is not in Main.StoryBoard so you would have to change your line where you are creating the new controller like this

let storyBoard = UIStoryboard(name: "YourStoryBoardName", bundle:nil)
let attendance = storyBoard?.instantiateViewControllerWithIdentifier("attention") as! attendenceTableViewController
attendance.creden = Int(idTextField.text!)
self.navigationController?.pushViewController(attendance, animated: true)

NOTE: YourStoryBoardName would be your StoryBoardName

Comments