iOS开发中,普遍遇到两个界面、类中需要传值的情况。譬如有两个视图控制器FirstViewController和SecondViewController,FirstViewController通过调用导航控制器的push方法push到SecondViewController。当SecondViewController中进行某项操作的时候,需要回调FirstViewController中的方法,修改FirstViewController中的属性。遇到这种情况,我们通常有两种方式。
方式一:使用delegate。
首选在SecondViewController中定义一个协议,
@protocol WGSecondViewControllerDelegate <NSObject>
- (void)delegateMethod;
@end
然后定义一个属性delegate:
@property(nonatomic,strong) id<WGSecondViewControllerDelegate> delegate;
接下来,在返回按钮的响应方法中,调用delegate方法:
- (IBAction)back:(id)sender {
//调用delegate方法
if ([_delegate respondsToSelector:@selector(delegateMethod)]) {
[_delegate performSelector:@selector(delegateMethod)];
}
[self.navigationController popViewControllerAnimated:YES];
}
接下来,在
FirstViewController引入该协议,实现这个delegate定义的方法:
// Created by Wu on 14-5-19.
// Copyright (c) 2014年 高 吴. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "WGSecondViewController.h"
@interface WGViewController : UIViewController<WGSecondViewControllerDelegate>
@property(nonatomic,strong) WGSecondViewController * wgSecondViewController;
@end
然后实现该方法:
- (void)delegateMethod;
{
NSLog(@"代理的方法得到了响应");
}
运行,得到:
2014-05-19 15:52:29.974 UINavigationDemo[3619:90b] 代理的方法得到了响应
好了,上面是使用delegate在不同界面之间传值的方法,接下来介绍block在不同界面之间传值的过程。两者比较类似,但是block理解起来稍微困难点。
首先,在WGSecondViewController 中定义一个block:
#import <UIKit/UIKit.h>
typedef void (^myBlock)(NSString *parameter);
@interface WGSecondViewController : UIViewController
@property(nonatomic,strong) myBlock block1;
- (IBAction)back:(id)sender;
- (void) receivedWithBlock:(myBlock)myBlockclass; //定义一个方法用来传递block
@end
然后,在WGSecondViewController.m文件中,实现以下方法:
// Created by Wu on 14-5-19.
// Copyright (c) 2014年 高 吴. All rights reserved.
//
#import "WGSecondViewController.h"
@interface WGSecondViewController ()
@end
@implementation WGSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
//开始传值
- (void) receivedWithBlock:(myBlock)myBlockclass;
{
NSString * name = @"http://blog.csdn.net/wugao2013";
NSLog(@"开始传值 ,myBlockclass=%@",myBlockclass);
if (myBlockclass) {
myBlockclass(name);
myBlockclass = nil;
}
}
- (IBAction)back:(id)sender {
//[self receivedWithBlock:_block1];
//上句相当于下面两句
// __block NSString * name = @"http://blog.csdn.net/wugao2013";
// _block1(name);
[self.navigationController popViewControllerAnimated:YES];
}
@end
然后在WGViewController.m中实现block:
- (void)cameraItemAction:(id)sender;
{
// 这里相当于设置delegate
[_wgSecondViewController setBlock1:^(NSString *msg){
NSLog(@"传过来的值为:%@",msg);
}];
[self.navigationController pushViewController:_wgSecondViewController animated:YES];
}
{
// 这里相当于设置delegate
[_wgSecondViewController setBlock1:^(NSString *msg){
NSLog(@"传过来的值为:%@",msg);
}];
[self.navigationController pushViewController:_wgSecondViewController animated:YES];
}
最后,运行一下,顺利的得到了wgSecondViewController界面传过来的值:
2014-05-19 16:19:55.243 UINavigationDemo[3675:90b] 开始传值 ,myBlockclass=<__NSGlobalBlock__: 0x5070>
2014-05-19 16:19:55.244 UINavigationDemo[3675:90b] 传过来的值为: http://blog.csdn.net/wugao2013
2014-05-19 16:19:55.244 UINavigationDemo[3675:90b] 传过来的值为: http://blog.csdn.net/wugao2013