Calling methods between view controllers using Storyboard

不羁的心 提交于 2019-12-08 13:09:02

问题


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


回答1:


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];



回答2:


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.



来源:https://stackoverflow.com/questions/6320587/calling-methods-between-view-controllers-using-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!