协议传值
来源:互联网 发布:python教程 图灵 编辑:程序博客网 时间:2024/06/07 15:13
协议传值是针对从后往前传值
先要自己定义协议 并且写协议方法 通过协议方法进行传值
//协议传值的第一步// 1.声明一份协议@protocol SecondViewControllerDelegate <NSObject>//协议方法-(void)changeValue:(NSString *)value;@end//在SecondViewController中声明协议@interface SecondViewController : UIViewController//2.设置代理人的属性@property(nonatomic,assign)id<SecondViewControllerDelegate>delegate;@end
属性传值在.m文件中需要写的内容,控件的基本内容不再详述
@interface SecondViewController ()//一个textfield,一个button@property(nonatomic,retain)UITextField *textField;@property(nonatomic,retain)UIButton *button;@end@implementation SecondViewController-(void)dealloc{ [_button release]; [_textField release]; [super dealloc];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self.view addSubview:self.textField]; self.view.backgroundColor = [UIColor lightGrayColor]; self.button = [UIButton buttonWithType:UIButtonTypeSystem]; [self.button setTitle:@"返回" forState:UIControlStateNormal]; [self.view addSubview:self.button]; [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];}//点击返回//协议的触发条件是点击按钮,所以在这里面进行协议传值的第三步//3.设置代理人执行的协议方法-(void)click:(UIButton *)button{ [self.navigationController popToRootViewControllerAnimated:YES];//传的值是自定义的textField里面的内容 [self.delegate changeValue:self.textField.text]; }
实现协议传值
#import "SecondViewController.h"//4.签订协议@interface MainViewController ()<SecondViewControllerDelegate>@property(nonatomic,retain)UILabel *lable;@property(nonatomic,retain)UIButton *button;@end@implementation MainViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.lable = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 150, 40)]; [self.lable release]; self.button = [UIButton buttonWithType:UIButtonTypeSystem]; [self.button setTitle:@"下一页" forState:UIControlStateNormal]; [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; self.navigationController.navigationBar.translucent = NO; }-(void)click:(UIButton *)button{//push下一页 SecondViewController *secVC = [[SecondViewController alloc]init]; [self.navigationController pushViewController:secVC animated:YES]; [secVC release];//5.设置代理人 secVC.delegate = self;}//6.实现协议方法-(void)changeValue:(NSString *)value{ NSLog(@"%@",value);//协议传的值赋值给自定义的label self.lable.text = value;}
0 0
- iOS 协议传值
- ios 协议传值
- 协议传值
- 协议传值
- 协议传值
- 界面传值(协议)
- 协议传值
- UI_协议传值
- 协议传值
- UI_协议传值
- iOS-协议传值
- 协议传值
- 协议、反向传值
- UI中协议传值
- IOS中协议传值
- iOS 代理协议传值
- 如何实现协议传值
- UITableView的协议传值
- hdu 5391 (数论)
- 从左往右 从上往下 按层遍历 输出 树 二叉树
- 电脑显示U盘,但是读取不了
- FrameLayout中控件使用属性:layout_weight
- 归并排序 MergeSort
- 协议传值
- scala实现设计模式之组合模式
- Android底层原理之从Service_manager源码分析Android进程间通信过程
- markdown 模板
- 欢迎使用CSDN-markdown编辑器
- Hibernate学习笔记(十) — HQL查询
- repr()与str()
- 10.2-7 O(n)非递归逆转单向链表的n个元素。
- 初学Java多线程:向线程传递数据的三种方法