导航控制器下UIViewController之间的传值 ------ 代理(delegate)传值 单例传值 Target-Action传值 属性传值

时间:2023-01-09 20:40:10

原文链接http://www.csdn123.com/html/mycsdn20140110/78/78bcf591e1d768ab4d12e612054ea2af.html



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];
}