ios 协议传值
来源:互联网 发布:楼市崩盘的后果 知乎 编辑:程序博客网 时间:2024/06/07 20:42
协议传值(作为回调比较实用)
如果单单是一个页面跳转传值的话 直接用属性来传值就很快 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
- iOS 协议传值
- ios 协议传值
- iOS-协议传值
- IOS中协议传值
- iOS 代理协议传值
- iOS ---反向传值 协议传值
- iOS 开发之协议-代理传值
- IOS开发中非正式协议传值
- ios之协议传值(Protocol)
- iOS 开发之协议-代理传值
- iOS 开发之协议-代理传值
- iOS开发之协议:代理传值
- iOS 开发之协议-代理传值
- iOS 开发之协议-代理传值
- iOS协议传值封装总结
- iOS开发之协议:代理传值
- ios反向传值--代理协议
- iOS协议传值与Block传值
- 倒水问题
- php面向对象_get(),_set()的用法
- 模拟 shell 写日志
- 黑盒测试网上学习资料
- 数据文件脱机
- ios 协议传值
- 孙鑫第5章 遇到error c2593
- java基础知识——面向对象
- SharePoint 2013 安装图解
- Android 蓝牙聊天室之官方例子
- linux 内核模块 参数
- 【解决显示器颜色不正常的办法】
- cocos2d-x Box2d 入门例子
- Android数据库基本操作(附源码)