简体   繁体   中英

Calling methods between view controllers using Storyboard

I've already read the post Passing data to views using Storyboards, but still have problem with calling methods:

Now I have AViewController and BViewController , both connected with Storyboard (no XIBs).

In BViewController:

-(void)doSth:(int)num;

So how can I call the doSth: method in AViewController ?

The old way like [bViewController doSth:123]; don't work because I can't get the instance of BViewController in the Storyboard.

Thanks.

Peak

I found if i use:

MapViewController *mapView = [self.storyboard instantiateViewControllerWithIdentifier:@"MapView"];

("MapView" is the identifier i set for that scene/view in storyboard)

and i just tested the old way and it works for me:

mapViewController *mapView = [[mapViewController alloc] initWithNibName:@"mapViewController" bundle:[NSBundle mainBundle]];

i can call methods in MapViewController directly, aka

[mapView testMethod];

You need to implement the prepareForSegue:sender: method in your view controller. The first paramter is a UIStoryboardSegue object which has references to both the source and destination view controller. This should allow you to do your [bViewController doSth:123]; pretty much as before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM