I am working on Swift, I've got an error in tableview's didSelectRowAtIndexPath method. I want to pass a value to another view controller i.e 'secondViewController'. Here EmployeesId is an Array. The relevant code is as follows:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var view: Dashboard = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as Dashboard
self.navigationController?.pushViewController(view, animated: true)
secondViewController.UserId = employeesId[indexPath.item] //getting an error here.
}
But I am getting this Error: fatal error: unexpectedly found nil while unwrapping an Optional value.
Any help will be appreciated.
Here's a general solution with two assumptions. First, UserId is not a UILabel. Second, you meant to use view
which was instantiated in the second line, instead of using secondViewController
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var view: Dashboard = self.storyboard?.instantiateViewControllerWithIdentifier("Dashboard") as Dashboard
self.navigationController?.pushViewController(view, animated: true)
view.UserId = employeesId[indexPath.row]
}
Here's what Dashboard looks like:
class Dashboard: UIViewController {
var UserId: String!
@IBOutlet var userIDLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
userIDLabel.text = UserId
}
...
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments