跨界面传值

来源:互联网 发布:企业网站用什么cms 编辑:程序博客网 时间:2024/06/16 02:27

我们传值常用的方法主要有四种:
1.属性传值
2.代理传值
3.block传值
4.通知传值


1.属性传值

属性传值应该来说是比较简单的一种传值方式,但是这种传值方式有其局限性,常用的一种场合是我们从界面A跳转到界面B,如何我们想讲界面A的值传到界面B,属性传值是比较方便的一种方式。如下图所示,如果我们点击A界面上的一个按钮,跳转到B界面,并且把A界面的一个值传送到B界面。

先说明一下大致的原理,首先要创建两个控制器A和B,在A中导入B的头文件,在A的按钮点击事件中,添加A跳转到B的代码段。现在的问题是如何在跳转的过程中把A界面上的值传到B界面呢?

我们可以给B添加一个属性,在点击按钮从A跳转到B的时候,将A界面要传送的值赋给B的属性,这样在B界面可以使用(self.属性)直接获取从A界面传过来的值。
代码段如下:(下面的代码段是一个最简单的演示)

1 #import <UIKit/UIKit.h>2 3 @interface ViewController : UIViewController4 {5     NSString *send;//我们在界面跳转的时候,将send的值传到下一个界面6 }7 - (IBAction)changeScreenButtonClick:(UIButton *)sender;8 @end
 1 #import "ViewController.h" 2 #import "BViewController.h" 3 @interface ViewController () 4  5 @end 6  7 @implementation ViewController 8  9 - (void)viewDidLoad10 {11     [super viewDidLoad];12     // Do any additional setup after loading the view, typically from a nib.13     send = @"传值操作";14 }15 16 - (void)didReceiveMemoryWarning17 {18     [super didReceiveMemoryWarning];19     // Dispose of any resources that can be recreated.20 }21 22 - (IBAction)changeScreenButtonClick:(UIButton *)sender23 {24     BViewController *b = [[BViewController alloc]init];25     b.receiveValue = send;26     UIWindow *window = [UIApplication sharedApplication].delegate.window;27     window.rootViewController = b;28 }29 @end
1 #import <UIKit/UIKit.h>2 3 @interface BViewController : UIViewController4 @property(nonatomic,copy) NSString *receiveValue;//接收A界面传过来的值5 @end
 1 #import "BViewController.h" 2  3 @interface BViewController () 4  5 @end 6  7 @implementation BViewController 8  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil10 {11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12     if (self) {13         // Custom initialization14     }15     return self;16 }17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     // Do any additional setup after loading the view from its nib.22     NSLog(@"从A界面传过来的值为:%@",self.receiveValue);23 }24 25 - (void)didReceiveMemoryWarning26 {27     [super didReceiveMemoryWarning];28     // Dispose of any resources that can be recreated.29 }30 31 @end

2.代理传值:

使用代理传值,我们需要知道怎么自定义代理,首先先普及一下代理的相关知识。

假如我们需要在A界面传值到B界面,我们需要在A界面中定义一些协议方法,只需要声明方法即可,不需要实现,如果其他类想要访问这些协议方法,只需要遵守这些协议即可。在A中定义的协议方法,相当于一个接口,你想使用A的接口,就要遵守A的协议方法,例如在B中,想要访问A的协议方法,B就要遵守A的协议。传值的话,B从A的协议方法中就能获取到A界面中的值。

如何自己写一个协议,下面是基本的格式:

1 @protocol 协议名称 <NSObject> 2 //协议方法 3 @end

假设我们现在要在A界面中写一个协议,具体代码如下(传值是从界面A传到界面B):

 1 #import <UIKit/UIKit.h> 2   3 //协议 4 @protocol aScreenDelegate <NSObject> 5 -(void)sendValueFromScreenaTOScreenb:(NSString *)value; 6 @end 7  8 @interface ViewController : UIViewController 9 @property(nonatomic,assign)id<aScreenDelegate>delegate;10 - (IBAction)changeScreenButtonClick:(UIButton *)sender;11 @end
 1 #import "ViewController.h" 2 #import "BViewController.h" 3 @interface ViewController () 4  5 @end 6  7 @implementation ViewController 8  9 - (void)viewDidLoad10 {11     [super viewDidLoad];12     // Do any additional setup after loading the view, typically from a nib.13 }14 15 - (void)didReceiveMemoryWarning16 {17     [super didReceiveMemoryWarning];18     // Dispose of any resources that can be recreated.19 }20 21 - (IBAction)changeScreenButtonClick:(UIButton *)sender22 {23     BViewController *b = [[BViewController alloc]init];24     NSString *send = @"wyg";25     self.delegate = b;26     [_delegate sendValueFromScreenaTOScreenb:send];27     [self.navigationController pushViewController:b animated:YES];28 }29 @end
1 #import <UIKit/UIKit.h>2 #import "ViewController.h"3 @interface BViewController : UIViewController<aScreenDelegate>4 @end
 1 @implementation BViewController 2  3 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 4 { 5     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 6     if (self) { 7         // Custom initialization 8     } 9     return self;10 }11 -(void)sendValueFromScreenaTOScreenb:(NSString *)value12 {13     NSLog(@"从A界面传过来的值:%@",value);14 }15 - (void)viewDidLoad16 {17     [super viewDidLoad];18     // Do any additional setup after loading the view from its nib.19     20 }

假如你向从界面B,pop到界面A,并把界面B的值传到界面A,这种使用属性传值不太方便,使用代理可以解决,从B界面往A传值,协议方法应该在B中写明,在A中遵守协议,下面是具体代码:

1 #import <UIKit/UIKit.h>2 #import "BViewController.h"3 @interface ViewController : UIViewController<bScreenDelegate>4 - (IBAction)changeScreenButtonClick:(UIButton *)sender;5 @end
 1 #import "ViewController.h" 2   3 @interface ViewController () 4  5 @end 6  7 @implementation ViewController 8  9 - (void)viewDidLoad10 {11     [super viewDidLoad];12     // Do any additional setup after loading the view, typically from a nib.13 }14 //代理方法15 -(void)sendValueFromBtoA:(NSString *)str16 {17     NSLog(@"从B界面传过来的值为:%@",str);18 }19 - (IBAction)changeScreenButtonClick:(UIButton *)sender20 {21     BViewController *b = [[BViewController alloc]init];22     b.delegate = self;23     [self.navigationController pushViewController:b animated:YES];24 }25 @end
 1 #import <UIKit/UIKit.h> 2  3 @protocol bScreenDelegate <NSObject> 4 -(void)sendValueFromBtoA:(NSString *)str; 5 @end 6  7 @interface BViewController : UIViewController 8 @property(nonatomic,assign) id<bScreenDelegate>delegate; 9 - (IBAction)popButtonClick:(UIButton *)sender;10 @end
 1 #import "BViewController.h" 2  3 @interface BViewController () 4  5 @end 6  7 @implementation BViewController 8  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil10 {11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12     if (self) {13         // Custom initialization14     }15     return self;16 }17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     // Do any additional setup after loading the view from its nib.22     23 }24 - (IBAction)popButtonClick:(UIButton *)sender25 {26     NSString *str = @"wxy";27     [_delegate sendValueFromBtoA:str];28     [self.navigationController popViewControllerAnimated:YES];29 }30 @end

3.block传值

使用block传值,我们需要自定义代理,这种写法相对来说是比较麻烦的,使用block传值的话,会使得代码量大大缩减,现在我们假设我们要把界面A上的值传到界面B,使用block来实现。

现在假设我们想在界面A上直接获取界面B上的信息,如何获取,代码如下:

1 #import <UIKit/UIKit.h>2 #import "BViewController.h"3 @interface ViewController : UIViewController4 @end
 1 #import "ViewController.h" 2 #import "BViewController.h" 3 @interface ViewController () 4  5 @end 6 @implementation ViewController 7  8 - (void)viewDidLoad 9 {10     [super viewDidLoad];11     // Do any additional setup after loading the view, typically from a nib.12     BViewController *b = [[BViewController alloc]init];13     [b converyValueToA:^(NSString *str) {14         NSLog(@"B界面上的值为:%@",str);15     }];16 }17 @end
1 #import <UIKit/UIKit.h>2 @interface BViewController : UIViewController3 -(void)converyValueToA:(void(^)(NSString *))block;4 @end
 1 #import "BViewController.h" 2  3 @interface BViewController () 4  5 @end 6  7 @implementation BViewController 8  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil10 {11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12     if (self) {13         // Custom initialization14     }15     return self;16 }17 -(void)converyValueToA:(void (^)(NSString *))block18 {19     NSString *name = @"wxy";20     block(name);21 }22 - (void)viewDidLoad23 {24     [super viewDidLoad];25     // Do any additional setup after loading the view from its nib.26     27 }28 29 @end

4.通知传值

通知传值有点类似于广播,有发送者,有监听者,比如A想接受B的值,B要发送一个通知,A只要监听这个通知,就能接收到值,通知传值就不细述。

例如发送一个通知:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];[center postNotificationName:通知名字 object:self userInfo:传递参数];

接收一个通知:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter];[center addObserver:self selector:@selector(receiveNofitication:) name:通知名字 object:nil];-(void)receiveNofitication:(NSNotification *)notification

{//接收到通知,执行相关代码}

注:一些简单的iOS基础知识,希望可以帮到像我一样的初学者。。T.T

1 0
原创粉丝点击