1 穿件一个协议类的头文件
#import <Foundation/Foundation.h>
@protocol conputer <NSObject>
-(void)sendvalue:(NSString *)value;
@end
2 viewcontroller头部文件中实现该类协议
#import <UIKit/UIKit.h>
#import "conputer.h"
@interface ViewController : UIViewController<conputer>
//实现代理的协议
@property (retain,nonatomic)id<conputer>heheda;
@property (nonatomic,strong)UIButton * eewButton;
@property (nonatomic,strong)NSString *str1;
@end
3在viewcontroller.m文件中初始化一个按钮,响应跳转到另一页,响应方法位
-(void)newAction:(UIButton *)btn
{
secondViewController *senc = [[secondViewController alloc]init];
self.heheda = senc;
//实现代理传值
[self.heheda sendvalue:@"thish"];
[self.navigationController pushViewController:senc animated:YES];
}
4在新建一个viewcontroller,作为按钮跳转的下一页
在头部文件中实现之前定义的协议
#import <UIKit/UIKit.h>
#import "conputer.h"
@interface secondViewController : UIViewController<conputer>
@property (retain,nonatomic)id<conputer>heheda;
@property (nonatomic,strong)NSString *str;
@end
5在.m实现文件中
#import "secondViewController.h"
@interface secondViewController ()
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self sendvalue:_str];
NSLog(@"%@",_str);
// Do any additional setup after loading the view.
}
-(void)sendvalue:(NSString *)value
{
_str = value;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}