ios 协议传值

协议传值(作为回调比较实用)

如果单单是一个页面跳转传值的话 直接用属性来传值就很快     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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值