Reputation: 69
I'm a beginner in IOS programming and in whole programming.
(I have a XCODE 6.4)
I have a code in my TableViewController in which a have a data, which I want to pass to another viewController. I was reading a lot about it in internet and than I have written this code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "formuleTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath) as! formule
let formuleCommand = formulesList[indexPath.row]
// Configure the cell...
var shortCut = formuleCommand.formuleText
cell.formuleLabel.text = shortCut
return cell
}
var valueToPass:String!
func tablView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow();
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as UITableViewCell!;
let identifier = "formuleTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(identifier, forIndexPath: indexPath!) as! formule
valueToPass = cell.formuleLabel.text
performSegueWithIdentifier("detail", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "detail") {
// initialize new view controller and cast it as your view controller
var viewController = segue.destinationViewController as! specialitiesViewController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
}
In my TableViewController I have cells, that contains labels with some data. I want to pass data from this labels to another ViewController. And when i run my app, there is no errors, but when I click on one of the cells, which have to move me to another ViewController (I have setted a segue for this) prints an error : fatal error: unexpectedly found nil while unwrapping an Optional value
And app crashes. Help me to fix this error please!
Thank you for your suggestions!
Upvotes: 0
Views: 1827
Reputation: 92
I would recommend using this code to open up your view controller (If you have a class for the VC you want to open).
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("your_Storyboard_id") as! Your_VC_Class
self.navigationController?.pushViewController(viewController, animated: true)
Upvotes: 1
Reputation: 10283
If you won't know the value of valueToPass
at initialization but may know the value at some time later, you should make valueToPass an optional:
var valueToPass: String?
...and then deal with unwrapping it when you need it and handling the case where it's not initialized appropriately.
If you won't know the value of valueToPass
at initialization but will know the value by the time the view controller is presented, then you should leave it as an implicitly unwrapped optional (i.e. as is) but ensure you set it to a valid value before use.
Side note: Generally speaking it's better to us as?
than as!
and handle the case where the cast did not work.
Upvotes: 0
Reputation: 12565
You've got valueToPass
defined as an implicitly unwrapped optional - i.e., you're telling iOS there is always going to be a value here.
So when prepareForSegue
is called, and you haven't set valueToPass
to anything, it will fail. You should initialise it with some value.
Upvotes: 1