1.代理(delegate)传值 ---- 顾名思义就是委托别人办事,就是当一件事情发生后,本身不处理,让别人来处理。
实质就是:好比右AB两个页面,A想要传值给B ,就只要先在A中获得B的指针,而后将想要传的值赋给B,以后跳转async
代码以下:post
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]; } // B实现协议里面的方法 -(void)showViewGiveValue:(NSString *)text{ _inputLabel.text = text; }
2.单例传值 ------- 若是页面之间相隔不少,要进行传值,将值保存到第三方,将第三方设置为单例模式atom
代码以下:spa
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;.net
3.Target-Action传值
实质就是:A页面要给B页面传值,A就提供接口出去,抓A到B内部来,A间接调用本身内部方法(至关于,A把本身内部须要操做的方法,传到B内来,到B内部进行赋值,这样就不存在访问不到各自的局部实例变量)代理
@property (nonatomic,assign)id target;
@property (nonatomic,assign)SEL action;
[self.traget performSelector:self.action withObject:nil];(是否须要传参数本身定,最多2个)指针
代码以下:code
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.属性/方法传值orm
A - pushViewController - B 在A视图中有B视图的对象 ,可直接操做server
-(void)didClickRightButtonItemAction { InputViewController * inputVC = [[InputViewController alloc] init]; [self.navigationController pushViewController:inputVC animated:YES]; }
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]; }
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]; }