I have two view controllers: BSViewController
which contains the source ivars number
and array
, and BSotherViewController
You're using a storyboard segue. The segue will create the destination view controller by loading it from the storyboard. There's no point in creating a -[BSotherViewController initWithNumber:array:]
method, because the segue won't use it.
When the user triggers the segue by tapping the button, the system creates an instance of UIStoryboardSegue
. This segue object has a destinationViewController
property which will (in your case) be the BSotherViewController
instance that is about to appear. The system sends a prepareForSegue:sender:
message to the source view controller (your BSViewController
), and passes the segue object as the first argument.
You need to implement prepareForSegue:sender:
in BSViewController
. In that method, you have access to both the source view controller (self
) and the destination view controller (segue.destinationViewController
), so you can pass data from one to the other.
First, add number
and array
properties to BSotherViewController
. Then, add a method like this to BSViewController
:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// The identifier here must match the one you assigned to the segue in the storyboard.
if ([segue.identifier isEqualToString:@"GoingToOtherViewController"]) {
BSotherViewController *destination = segue.destinationViewController;
destination.number = self.number;
destination.array = self.array;
}
}
Finally, in -[BSotherViewController viewDidLoad]
, use the values of your new number
and array
properties to set the content of your views.