Reputation: 167
I am trying to link together 2 controllers with segue. I have a view controller with 6 button that after the click on a button will show a tableviewcontroller
. I made all the link, give the identifier to segue etc.
in the tableviewcontroller
there is an array that should show filtered results depending on which button trigger the segue
How should I write the prepareforsegue
method?
This is what i am doing
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "chooseBrand" {
if sender?.tag == 1 {
let destinationController = segue.destinationViewController as! SelectionTableViewController
let numberOfCameras = destinationController.Camera.count
}
the problem is that i receive this error message
"Could not cast value of type 'UINavigationController' (0x10a0ef588) to 'myApp.SelectionTableViewController' (0x108658b90)."
How can I define the content of the tableview
in the selectiontableviewcontroller
depending on which button is pressed?
Thanks a lot for any kind of help :)
Upvotes: 2
Views: 1614
Reputation: 1519
What exactly is the sender here? The error is saying that sender with tag = 1 is not a SelectionTableViewController.
Anyway, what you could do is simply to connect each button to the same action outlet method and the have a generic segue
between the two controllers. When you then hit button A, B, C, D, E, or F you simply check their tag
(or store them as variables in your controller class) and send whatever you want to the controller. It's important that the segue you have between the controllers is between two controllers and not a segue
from each UIButton
the other controller. In the example below I assume you store each of the six UIButton as class variables.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segueFromFirstToSecond" {
let destinationController = segue.destinationViewController as! SelectionTableViewController
if sender = button1 {
let numberOfCameras = destinationController.Camera.count
} else if sender == button2 {
let numberOfCameras = destinationController.Camera.count
}
}
@IBAction func buttonPressed(sender: AnyObject) {
performSegueWithIdentifier(segueProfileToPul, sender: sender)
}
Connect all buttons to the buttonPressed()
. I don't think this line let numberOfCameras = destinationController.Camera.count
works since the SelectionTableViewController is not instantiated yet and thus has no values in its variables (I think). What you are looking for are probably setting this value so destinationController.Camera.count = someValue
. Maybe I'm wrong though.
Edit I uploaded a test project showing what I mean. Pay attention to the connections I've made in storyboard. Download here
Upvotes: 2