ios中几种传值方式
来源:互联网 发布:手机淘宝账号交易平台 编辑:程序博客网 时间:2024/06/05 11:41
Target-Action传值
实质就是:A页面要给B页面传值,A就提供接口出去,抓A到B内部来,A间接调用自己内部方法(相当于,A把自己内部需 要操作的方法,传到B内来,到B内部进行赋值,这样就不存在访问不到各自的局部实例变量)
@property (nonatomic,assign)id traget; @property (nonatomic,assign)SEL action;
[self.traget performSelector:self.action withObject:nil];(是否需要传参数自己定,最多2个)
代码如下:- #import "FirstViewController.h"
- #import "SecondViewController.h"
- #import "UIButton+Create.h"
- @interface FirstViewController ()
- {
- UILabel * _label;
- }
- @end
- @implementation FirstViewController
- - (void)dealloc
- {
- [_label release];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor redColor];
- self.navigationItem.title = @"首页";
- _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _label.backgroundColor = [UIColor greenColor];
- // _label.text = self.text;
- [self.view addSubview:_label];
- UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
- [self.view addSubview:button];
- // Do any additional setup after loading the view.
- }
- - (void)didClickButtonAction
- {
- SecondViewController * secondVC = [[SecondViewController alloc]init];
- secondVC.target = self;
- secondVC.action = @selector(didClick:);
- [self.navigationController pushViewController:secondVC animated:YES];
- [secondVC release];
- }
- - (void)didClick:(NSString *)text
- {
- _label.text = text;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- #import <UIKit/UIKit.h>
- @interface SecondViewController : UIViewController
- @property (nonatomic,assign)id target;
- @property (nonatomic,assign)SEL action;
- @end
- #import "SecondViewController.h"
- #import "FirstViewController.h"
- #import "UIButton+Create.h"
- @interface SecondViewController ()
- {
- UITextField * _textField;//创建一个输入框
- }
- @end
- @implementation SecondViewController
- - (void)dealloc
- {
- [_textField release];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor orangeColor];
- self.navigationItem.title = @"第二页";
- _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _textField.borderStyle = UITextBorderStyleRoundedRect;
- [self.view addSubview:_textField];
- UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];
- [self.view addSubview:button];
- // Do any additional setup after loading the view.
- }
- - (void)didClickButtonAction
- {
- [_target performSelector:_action withObject:_textField.text];
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
block传值
其实block传值个人感觉跟代理很相似.也是从后往前传.
//流程:
1.后一个界面定义一个block,并且定义一个属性block
2.在后一个界面返回前一个界面的瞬间,(即:创建完成一个界面之后),调用block;
3.前一个界面实现block的实现
4.后一个界面在合适的机会, 让(传的值以参数的形式 含在block的参数里)
1.后一个界面定义一个block,并且定义一个属性block
2.在后一个界面返回前一个界面的瞬间,(即:创建完成一个界面之后),调用block;
3.前一个界面实现block的实现
4.后一个界面在合适的机会, 让(传的值以参数的形式 含在block的参数里)
代码如下:
- <pre name="code" class="objc">#import "FirstViewController.h"
- #import "SecondViewController.h"
- #import "UIButton+Create.h"
- @interface FirstViewController ()
- {
- UILabel * _label;
- }
- @end
- @implementation FirstViewController
- - (void)dealloc
- {
- [_label release];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor redColor];
- self.navigationItem.title = @"首页";
- self.view.userInteractionEnabled = YES;
- /**
- * 1.创建一个UIButton,
- * 2.并添加响应事件,从首页跳转到第二个页面.
- */
- UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
- button.userInteractionEnabled = YES;
- [self.view addSubview:button];
- /**
- * 1.在第1个界面创建一个UILabel
- * 2.把第二页输入框输入的字符串,通过block内部实现传过来
- * 3.然后通过赋值给UILabel
- */
- _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _label.backgroundColor = [UIColor greenColor];
- [self.view addSubview:_label];
- // Do any additional setup after loading the view.
- }
- - (void)didClickButtonAction
- {
- /**
- * 1.用push的方法推出下一个页面
- * 2.把第二页输入框输入的字符串,通过block内部实现传过来
- * 3.从而实现把输入框输入的字符串,传到UILabel上.
- */
- SecondViewController * secondVC = [[SecondViewController alloc]init];
- secondVC.blocks=^(NSString * str){
- _label.text = str;
- };
- [self.navigationController pushViewController:secondVC animated:YES];
- [secondVC release];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- <pre name="code" class="objc">#import <UIKit/UIKit.h>
- typedef void(^block)(NSString * str) ;
- @interface SecondViewController : UIViewController
- @property (nonatomic,copy)block blocks;
- @end
- #import "SecondViewController.h"
- #import "UIButton+Create.h"
- #import "FirstViewController.h"
- @interface SecondViewController ()
- {
- UITextField * _textField;//创建一个输入框
- }
- @end
- @implementation SecondViewController
- - (void)dealloc
- {
- [_textField release];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor orangeColor];
- self.navigationItem.title = @"第二页";
- /**
- * 1.创建一个UIButton,
- * 2.并添加响应事件,从第二个页面返回到首页.
- */
- UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];
- [self.view addSubview:button];
- /**
- * 1.在第二个界面创建一个输入框
- *
- */
- _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _textField.borderStyle = UITextBorderStyleRoundedRect;
- [self.view addSubview:_textField];
- // Do any additional setup after loading the view.
- }
- - (void)didClickButtonAction
- {
- //调用block方法,把<span style="font-family: Arial;">_textField.text作为参数传过去</span>
- _blocks(_textField.text);
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
属性/方法传值
//1.后面的界面定义了一个属性,用于保存,前一个界面,传过来的值
//注:属性定义成字符串还是别的类型,取决于你的需求,本例我们需要一个字符串,用于UILabel显示
//2.后面的界面创建完毕之后,为属性赋值,(即:记录需要传递的值)
//3.在需要使用值的地方,使用属性记录的值这种通过定义属性,达到传值的方式,称为属性传值,
//属性传值,一般用于从前一个界面向后一个界面传值;
代码如下:
- #import "FirstViewController.h"
- #import "SecondViewController.h"
- #import "UIButton+Create.h"
- @interface FirstViewController ()
- {
- UITextField * _textField;//创建一个输入框
- }
- @end
- @implementation FirstViewController
- - (void)dealloc
- {
- [_textField release];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor redColor];
- self.navigationItem.title = @"首页";
- /**
- * 1.在第一个界面创建一个输入框
- *
- */
- _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _textField.borderStyle = UITextBorderStyleRoundedRect;
- [self.view addSubview:_textField];
- /**
- * 1.创建一个UIButton,
- * 2.并添加响应事件,从首页跳转到第二个页面.
- */
- UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
- [self.view addSubview:button];
- // Do any additional setup after loading the view.
- }
- - (void)didClickButtonAction
- {
- /**
- * 1.用push的方法推出下一个页面
- * 2.把首页输入框输入的字符串,通过SecondViewController类的属性NSString * text接收
- * 3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上.
- */
- SecondViewController * secondVC = [[SecondViewController alloc]init];
- secondVC.text = _textField.text;
- [self.navigationController pushViewController:secondVC animated:YES];
- [secondVC release];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- #import "SecondViewController.h"
- @interface SecondViewController ()
- @end
- @implementation SecondViewController
- - (void)dealloc
- {
- [_label release];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor orangeColor];
- self.navigationItem.title = @"第二页";
- /**
- * 1.在第二个界面创建一个UILabel
- * 2.把首页输入框输入的字符串,通过SecondViewController类的属性NSString * text接收
- * 3.然后通过赋值给UILabel
- */
- _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _label.backgroundColor = [UIColor greenColor];
- _label.text = self.text;
- [self.view addSubview:_label];
- // Do any additional setup after loading the view.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
通知传值
//流程:
1.注册通知
2.通知中心,发送一条消息通知----------其中name名字千万不要写错了,会出现在3个地方
3.实现通知中心内部的方法,并实现传值
4.第四步,消息发送完,要移除掉
代码如下:
- #import "FirstViewController.h"
- #import "SecondViewController.h"
- #import "UIButton+Create.h"
- @interface FirstViewController ()
- {
- UILabel * _label;
- }
- @end
- @implementation FirstViewController
- - (void)dealloc
- {
- [_label release];
- //第四步,消息发送完,要移除掉
- [[NSNotificationCenter defaultCenter]removeObserver:self name:@"labelTextNotification" object:nil];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor redColor];
- self.navigationItem.title = @"首页";
- _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _label.backgroundColor = [UIColor greenColor];
- [self.view addSubview:_label];
- UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)];
- [self.view addSubview:button];
- //第二步,通知中心,发送一条消息通知----------其中name名字千万不要写错了,会出现在3个地方
- [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showLabelText:) name:@"labelTextNotification" object:nil];
- // Do any additional setup after loading the view.
- }
- - (void)showLabelText:(NSNotification *)notification
- {
- //第三,实现通知中心内部的方法,并实现传值
- id text = notification.object;
- _label.text = text;
- }
- - (void)didClickButtonAction
- {
- SecondViewController * secondVC = [[SecondViewController alloc]init];
- [self.navigationController pushViewController:secondVC animated:YES];
- [secondVC release];
- }
- - (void)didClick:(NSString *)text
- {
- _label.text = text;
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
- #import "SecondViewController.h"
- #import "FirstViewController.h"
- #import "UIButton+Create.h"
- @interface SecondViewController ()
- {
- UITextField * _textField;//创建一个输入框
- }
- @end
- @implementation SecondViewController
- - (void)dealloc
- {
- [_textField release];
- [super dealloc];
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.view.backgroundColor = [UIColor orangeColor];
- self.navigationItem.title = @"第二页";
- _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)];
- _textField.borderStyle = UITextBorderStyleRoundedRect;
- [self.view addSubview:_textField];
- UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Back" target:self action:@selector(didClickButtonAction)];
- [self.view addSubview:button];
- // Do any additional setup after loading the view.
- }
- - (void)didClickButtonAction
- {
- //第一步注册通知
- [[NSNotificationCenter defaultCenter]postNotificationName:@"labelTextNotification" object:_textField.text];
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
0 0
- ios中几种传值方式
- IOS中几种传值的方式
- iOS NSString 连接方式
- IOS Socket 同步方式
- iOS 页面跳转方式
- iOS 各种动画方式
- ios常见存储方式
- iOS 跳转的方式
- ios 注册打开方式
- IOS多线程实现方式
- IOS JSON解析方式
- iOS下载方式详解
- ios 传值方式
- IOS存储方式
- ios中五种回调方式
- ios图片拉升几种方式
- ios中的布局方式
- ios的两种传值方式
- 5、Cocos2d-X 游戏编程之------精灵创建的方式
- 求助(VC++) 隐藏Console窗口无效
- asyntask配合线程池使用
- App项目设计开发完整流程
- 《读不透劳动合同法,决不能做HR(新<劳动合同法>风险规避指南)》一书目录
- ios中几种传值方式
- [Win32]窗口的创建以及消息响应基本概念
- dbcp和c3p0数据源配置,建议用c3p0
- 九月学习感想
- ExtJS中表格控件的使用,属性设置和数据的获取加载
- 6、Cocos2d-X 游戏编程之------CCEditBox的实现
- 《数据结构(C++版)》第二章线性表思维导图
- main原型和参数含义
- 7、Cocos2d-X 游戏编程之------CCControlSwitch的实现