将数据传递给根视图控制器

时间:2014-05-05 01:53:31

标签: ios

我有一个简单的应用程序,具有以下结构:

  • ' A':带有WebView的独立视图控制器
  • ' B':导航控制器
  • ' C':根视图控制器' B',所有商店的TableView

当WebView的网址在' A'变成特定的东西(http://www.mysite.com/store),我需要:

  1. 按下' C'
  2. 发送要在' C'
  3. 中使用的字符串

    我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

Here you can do with delegate methods   
  

//这是你的根ViewController来调用委托方法

#import "ThirdViewController.h"
#import "SecondViewController.h"
 @interface ViewController ()<ThirdViewControllerDelegate>
  @end

@implementation ViewController
 #pragma mark -
 #pragma mark ThirdViewController Delegate Method
  

//在Root ViewController中实现委托方法

-(void)didSelectValue:(NSString *)value{
    NSLog(@"%@",value);
}
// Pass the last Vc delegate to the next ViewController
-(void)gotoSeconddVc{
    SecondViewController *vc2=[[SecondViewController alloc]init];
    vc2.lastDelegate=self;
    [self.navigationController pushViewController:vc2 animated:YES];
}


 #import "ThirdViewController.h"
     @interface SecondViewController : UIViewController
        @property(nonatomic,retain) id <ThirdViewControllerDelegate> lastDelegate;
        @end

-(void)gotoThirdVc{
    ThirdViewController *vc3=[[ThirdViewController alloc]init];
    vc3.delegate=self.lastDelegate;
    [self.navigationController pushViewController:vc3 animated:YES];
}
  

最后一个viewcontroller的实现

@implementation ThirdViewController


-(void)btnDoneClicked{
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.delegate didSelectValue:strValue]; //call delegate methods here
}