导航控制器下UIViewController之间的传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值 block传值 通知传值
1.代理(delegate)传值 ---- 顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。
实质就是:比如右AB两个页面,A想要传值给B ,就只要先在A中得到B的指针,然后将想要传的值赋给B,之后跳转
代码如下:
- A.h
- @protocol HMTShowViewControllerDelegate <NSObject>
- @optional
- - (void)showViewGiveValue:(NSString *)text;
- @end
- @interface HMTShowViewController : UIViewController <UITextFieldDelegate>
- @property (nonatomic,copy)NSString * text;
- // 定义一个代理
- @property (nonatomic,assign)id<HMTShowViewControllerDelegate> delegate;
- @end
- A.m
- - (void)popPerView:(UIBarButtonItem *)barButton{
- // 在页面跳转前将参数传出去
- if ([self.delegate respondsToSelector:@selector(showViewGiveValue:)]) {
- [self.delegate showViewGiveValue:_showTextField.text];
- }
- [self.navigationController popViewControllerAnimated:YES];
- }
- B.h
- @interface HMTInputViewController : UIViewController <HMTShowViewControllerDelegate>
- B.m
- - (void)pushNextViewControl:(UIBarButtonItem *)button{
- HMTShowViewController * showVC = [[HMTShowViewController alloc]init];
- showVC.text = _textField.text;
- // 将代理对象设置成B
- showVC.delegate = self;
- [self.navigationController pushViewController:showVC animated:YES];
- [showVC release];
- }
- // B实现协议里面的方法
- - (void)showViewGiveValue:(NSString *)text{
- _inputLabel.text = text;
- }
2.单例传值 ------- 如果页面之间相隔很多,要进行传值,将值保存到第三方,将第三方设置为单例模式
代码如下:
- static HMTInputViewController * shareRootViewController = nil;
- @implementation HMTInputViewController
- +(HMTInputViewController *)sharedController{
- @synchronized(self){
- if(shareRootViewController == nil){
- shareRootViewController = [[self alloc] init] ;
- }
- }
- return shareRootViewController;
- }
- 要将HMTInputViewController中的一个UITextField编辑的内容传到HMTShowViewController中的UILabel
- _showLabel.text = [HMTInputViewController sharedController].textField.text;
3.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.h
- @interface HMTShowViewController : UIViewController <UITextFieldDelegate>
- @property (nonatomic,assign) id traget;
- @property (nonatomic,assign) SEL action;
- @end
- A.m
- - (void)popPerView:(UIBarButtonItem *)barButton{
- [self.traget performSelector:self.action withObject:_showTextField.text];
- [self.navigationController popViewControllerAnimated:YES];
- }
- B.m
- - (void)pushNextViewControl:(UIBarButtonItem *)button{
- HMTShowViewController * showVC = [[HMTShowViewController alloc]init];
- showVC.traget = self;
- showVC.action = @selector(changeLabelText:);
- [self.navigationController pushViewController:showVC animated:YES];
- }
- - (void)changeLabelText:(NSString *)text{
- _inputLabel.text = text;
- }
4.属性/方法传值
A - pushViewController - B 在A视图中有B视图的对象 ,可直接操作
- - (void)didClickRightButtonItemAction
- {
- InputViewController * inputVC = [[InputViewController alloc] init];
- [self.navigationController pushViewController:inputVC animated:YES];
- [inputVC release];
- }
B - popViewControllerAnimated - A 因为A推出B,A只是隐藏了,退到栈底去了,在B返回A,A的viewDidLoad并不会再次调用
- - (void)didClickLeftButtonItemAction
- {
- ShowViewController * showView = (ShowViewController *)[self.navigationController.viewControllers objectAtIndex:0];
- [self.navigationController popViewControllerAnimated:YES];
- }
5.block传值(A-view 传值给 B-view)
@释放 Block_release(_doTransferMsg);
- A.h
- @property (nonatomic,copy) void(^doTransferMsg)(NSString * msg);
- A.m
- - (void)popPerView:(UIBarButtonItem *)barButton{
- if (_doTransferMsg) {
- _doTransferMsg(_showTextField.text);
- }
- [self.navigationController popViewControllerAnimated:YES];
- }
- B.m
- - (void)pushNextViewControl:(UIBarButtonItem *)button{
- HMTShowViewController * showVC = [[HMTShowViewController alloc]init];
- // block传值
- [showVC setDoTransferMsg:^(NSString * msg) {
- dispatch_async(dispatch_get_main_queue(), ^{
- _inputLabel.text = msg; });
- }];
- [self.navigationController pushViewController:showVC animated:YES];
- [showVC release];
- }
6.通知传值(同上,也是A传给B)
- A.m
- - (void)popPerView:(UIBarButtonItem *)barButton{
- [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabelTextNotification" object:_showTextField.text];
- [self.navigationController popViewControllerAnimated:YES];
- }
- B.m
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // 通知中心,发送一条消息传值--------其中name千万不要写错了,会出现在3个地方
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showViewGiveValue:) name:@"changeLabelTextNotification" object:nil];
- }
- - (void)showViewGiveValue:(NSNotification *)notification{
- // 取出notification的object
- id text = notification.object;
- _inputLabel.text = text;
- }
- -(void)dealloc{
- // 消息发送完,要移除掉
- [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLabelTextNotification" object:nil];
- [super dealloc];
- }
原文地址:http://blog.csdn.net/hmt20130412/article/details/22896839