如何与不同的视图控制器通信?

时间:2012-10-30 04:45:19

标签: iphone objective-c cocoa-touch

我有一个VC,它以模态方式呈现另一个VC。第二个VC推送到第三个VC。第三个VC解散自己,需要在初始VC上调用一个方法。我似乎无法让这个工作。有什么建议吗?

 VC3 *aVC3 = [[self.navigationController.viewControllers objectAtIndex:0] parentViewController];
 aVC3 = self.name;
 [aVC3 addExercise];
 [self dismissView];

2 个答案:

答案 0 :(得分:1)

delegate使用backward messsaging

参考simple-delegate-tutorial-for-ios-development。另请参阅the-basics-of-protocols-and-delegates

如果你有UINavigationControllerno need of delegate可以与navigationController中的任何viewController进行通信,如:

ViewController *objViewController = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0]; //need to which controller u would like to communicate
[objViewController yourMethodHere]; // your method here

答案 1 :(得分:-1)

这是另一种方法:

第1步:在FirstViewController

中创建SecondViewController.h媒体资源
@interface SecondaryViewController : UIViewController{
}
@property (nonatomic, strong) FirstViewController *viewControllerOne;

第2步:在self

中展示SecondViewController时,将该属性设置为FirstViewController.m
@implementation SecondaryViewController

-(void)functionThatPresentsSecondViewController{
    SecondViewController *viewControllerTwo = [[SecondViewController alloc] init];
    [viewControllerTwo setViewControllerOne: self];
    [self presentViewController:viewControllerTwo animated:NO completion:nil];
}

第3步:您现在可以使用FirstViewController属性从SecondViewController调用viewControllerTwo.viewControllerOne个函数。