target-action传值
2014-10-22 16:49
253 查看
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个)
代码例如以下:
实质就是: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
相关文章推荐
- iOS的Target-Action模式传值的最佳方式
- target-action传值
- 常见的6种传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值
- target-action传值
- 导航控制器下UIViewController之间的传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值
- iOS如何实现传值---target-action设计模式
- RCP 中 RetargetAction 的实现
- 【ios开发】浅谈Target-action模式
- <c:set>传值到action
- action传值给页面EL表达式${}接收总为null
- ios target-action响应方法的不同形式
- action标签传值
- UI Target/Action事例
- JS通过JSON传值给Action
- 在Struts2中jsp前台传值到action后台的方法(转载)
- Struts2中jsp前台传值到action后台的三种方式
- action、js传值
- 在Struts2中jsp前台传值到action后台的方法
- UI_Target-Action
- MVC中使用Ajax提交数据 Jquery Ajax方法传值到action