UI_协议传值
来源:互联网 发布:prisma算法 编辑:程序博客网 时间:2024/05/22 00:48
SecondViewController.h#import <UIKit/UIKit.h>1.声明一份协议.@protocol SecondViewControllerDelegate <NSObject>// 创建一个方法.- (void)takeValue:(NSString *)strValue;@end@interface SecondViewController : UIViewController2.声明代理人属性.@property(nonatomic, assign)id<SecondViewControllerDelegate>delegate;@end
SecondViewController.m#import "SecondViewController.h"#import "RootViewController.h"@interface SecondViewController ()@property(nonatomic, retain)UITextField *textField;@end@implementation SecondViewController- (void)dealloc{ [_textField release]; [super dealloc];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; // 创建textField. self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)]; self.textField.layer.borderWidth = 1; self.textField.layer.cornerRadius = 10; [self.view addSubview:self.textField]; [_textField release]; // 创建button. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 200, 150, 50); [button setTitle:@"返回" forState:UIControlStateNormal]; button.layer.borderWidth = 1; button.layer.cornerRadius = 10; [self.view addSubview:button]; [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];}- (void)backAction:(UIButton *)button { // 返回上一页. [self.navigationController popViewControllerAnimated:YES]; 3.设置代理人执行的协议方法 [self.delegate takeValue:self.textField.text]; }
RootViewController.m#import "RootViewController.h"#import "SecondViewController.h"4.签订协议@interface RootViewController ()<SecondViewControllerDelegate>@property(nonatomic, retain)UITextField *myTextField;@property(nonatomic, retain)UILabel *myLabel;@end@implementation RootViewController- (void)dealloc{ [_myTextField release]; [_myLabel release]; [super dealloc];}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 创建一个textField. self.myTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 150, 50)]; self.myTextField.layer.borderWidth = 1; self.myTextField.layer.cornerRadius = 10; [self.view addSubview:self.myTextField]; [_myTextField release]; // 创建label. self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 200, 150, 50)]; self.myLabel.layer.borderWidth = 1; self.myLabel.layer.cornerRadius = 10; [self.view addSubview:self.myLabel]; [_myLabel release]; // 创建一个下一页的button. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(100, 300, 150, 50); [button setTitle:@"下一页" forState:UIControlStateNormal]; button.layer.borderWidth = 1; button.layer.cornerRadius = 10; [self.view addSubview:button]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];}- (void)buttonAction:(UIButton *)button { // push下一页. SecondViewController *secondVC = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release];5.设置代理人. secondVC.delegate = self;}6.实现协议方法.- (void)takeValue:(NSString *)strValue { NSLog(@"%@", strValue); // 显示在label上 self.myLabel.text = strValue;}
0 0
- UI_协议传值
- UI_协议传值
- UI_协议
- UI_属性传值
- UI_页面传值问题
- UI_界面传值方式
- UI_组件
- UI_多线程
- UI_手势
- UI_手势
- UI_霓虹灯
- UI_动画
- UI_通讯录
- UI_数据库
- UI_封装
- UI_手势
- UI_涂鸦作业
- UI_自定义视图
- bones脚本篇 - 内置标签image
- Eclipse 转Android Studio 常见问题小结
- 【Unity3d开发基础】Mathf中的数学运算
- 使用jquery-form.js,直接提交表单至后台(springmvc)
- Js调用android方法
- UI_协议传值
- Objective-C与C++通信
- Java——多线程总结、ThreadLocal/Volatile/synchronized/Atomic关键字
- logcat命令详解
- php无刷新上传图片
- ThinkPHP--跨控制器调用
- Unity3d 碰撞器和触发器
- 汉诺塔问题,Python
- Selection和Range对象