您的位置:首页 > 移动开发 > IOS开发

iOS 协议传值

2013-09-29 16:43 232 查看
协议传值(作为回调比较实用)

如果单单是一个页面跳转传值的话 直接用属性来传值就很快     A--->B

好比说 A 跳转到 B 并且传值  在A页面就直接 b.property = @”hello“就直接传过去了  

但是  如果是 A跳转到B 然后 在B页面操作完后 B的数值传到A(回调)  那这就不能用上面那样了 这里的话用协议就很好用了  A---->B---->A

举个例子:

好比说 我在第一个页面是通讯录信息列表  

然后我点击添加按钮 

弹出一个页面 

[selfpresentViewController:vcanimated:YEScompletion:nil];
在第二个页面添加数据  添加完成后 

就返回到第一个页面 

 [selfdismissViewControllerAnimated:YEScompletion:nil];
 这时候用协议把数值回调到第一个页面就很方便了  

个人理解:

A: 

希望别人传来数值(叫别人做事) 这边接收的话 那就在该类中声明协议   在协议方法里面得到数值

//要得到(侧重回调)数据的话就声明协议

@interface ViewController :UIViewController<changeProtocolDelegate>

//(实现协议的方法的时候 得到回调的数据)
-(void)change_Value:(NSString *)value
{
   self.myLabel.text =value;
   NSLog(@"%@",value);
}
B:
要给别人传值(帮别人做事)就声明代理属性

//要给别人传值
就声明一个代理属性
@interface changeViewController :UIViewController

@property(retain,nonatomic)id<changeProtocolDelegate>delegate;
 传值

//帮别人做事 
(传一个数值 )

    [self.delegatechange_Value:self.txt.text];

A---->B----->A一个回调过程

代码

#import <UIKit/UIKit.h>
#import "changeViewController.h"
#import "changeProtocolDelegate.h"

//要得到(侧重回调)数据的话 就声明协议
@interface ViewController : UIViewController<changeProtocolDelegate>
@property (retain, nonatomic) IBOutlet UILabel *myLabel;
- (IBAction)changAction:(id)sender;
@end


#import "ViewController.h"

@interface ViewController()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)dealloc {
[_myLabel release];
[super dealloc];
}
//(实现协议的方法的时候  得到回调的数据)
-(void)change_Value:(NSString *)value
{
self.myLabel.text =value;
NSLog(@"%@",value);
}
- (IBAction)changAction:(id)sender {
changeViewController *vc = [[changeViewController alloc] init];
vc.delegate = self;
[self presentViewController:vc animated:YES completion:nil];
// [vc release];
}
@end


第二个页面

#import <UIKit/UIKit.h>
#import "changeProtocolDelegate.h"
#import "ViewController.h"
//要给别人传值 就声明一个代理属性
@interface changeViewController : UIViewController
@property(retain,nonatomic)id<changeProtocolDelegate>delegate;
@property (retain, nonatomic) IBOutlet UITextField *txt;
- (IBAction)changeAction:(id)sender;
@end


#import "changeViewController.h"

@interface changeViewController ()
@property(retain,nonatomic)ViewController *vc;
@end

@implementation changeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//    [vc.delegate changeValue:@"ddd"];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)dealloc {
[_txt release];
[_delegate release];
[super dealloc];
}
- (IBAction)changeAction:(id)sender {
//帮别人做事  (传一个数值 )
[self.delegate change_Value:self.txt.text];
[self dismissViewControllerAnimated:YES completion:nil];
}
@end


代码下载地址:http://download.csdn.net/detail/aa741649143/6339189
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: