iOS ViewController之间传值方法四(利用Delegate)

来源:互联网 发布:iphone 桌面软件管理 编辑:程序博客网 时间:2024/05/19 16:35

3.利用Delegate在两个或多个ViewController之间传值

在A界面

      .h文件

@protocol secondViewDelegate

-(void)passViewController:(NSString*)str;//1a.定义协议与方法

@end


@interface ViewController : UIViewController

@property(retain,nonatomic)id<secondViewDelegate>secondDelegate;//1b.定义向趋势页面传值的委托变量

@end

      .m文件

    SecondViewController *vc = [[SecondViewControlleralloc]init];

    self.secondDelegate = vc;//设置代理

    [self.secondDelegatepassViewController:@"this is a string!"];//

    [selfpresentViewController:vcanimated:YEScompletion:nil];


在B界面

      .h文件      

#import "ViewController.h"

@interface SecondViewController : UIViewController< secondViewDelegate>{


}

      .m文件

//实现传值协议方法

-(void)passViewController:(NSString *)str{

    NSLog(@"-----------%@",str);

}


注意:在定义代理的时候加上关键字@optional则表明这个代理可以不用实现所有的代理方法而不被报警告。
0 0
原创粉丝点击