A页面跳转到B页面,B页面向A页面传值。
Delegate
A 页面
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()<ReturnDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:@selector(nextViewController)];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)nextViewController{
SecondViewController *svc = [[SecondViewController alloc]init];
svc.delegate = self;
[self.navigationController pushViewController:svc animated:YES];
}
- (void)returnString:(NSString *)string {
NSLog(@"%@",string);
}
@end
B页面
#import <UIKit/UIKit.h>
@protocol ReturnDelegate <NSObject>
- (void)returnString:(NSString *)string;
@end
@interface SecondViewController : UIViewController
@property(nonatomic, weak)id<ReturnDelegate>delegate;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(goback)];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)goback{
if ([self.delegate respondsToSelector:@selector(returnString:)]) {
[_delegate returnString:@"6666666"];
}
[self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
Block
A页面
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:@selector(nextViewController)];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)nextViewController{
SecondViewController *svc = [[SecondViewController alloc]init];
svc.block = ^(NSString *string) {
NSLog(@"%@",string);
};
[self.navigationController pushViewController:svc animated:YES];
}
@end
B页面
#import <UIKit/UIKit.h>
typedef void(^ReturnBlock)(NSString *string);
@interface SecondViewController : UIViewController
@property(nonatomic, copy)ReturnBlock block;
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(goback)];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)goback{
if (self.block != nil) {
self.block(@"66666666");
}
[self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
Notification
A页面
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:@selector(nextViewController)];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"notificationtest" object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)nextViewController{
SecondViewController *svc = [[SecondViewController alloc]init];
[self.navigationController pushViewController:svc animated:YES];
}
- (void)receiveNotification:(NSNotification *)noti{
// NSNotification 有三个属性,name, object, userInfo,其中最关键的object就是从第三个界面传来的数据。name就是通知事件的名字, userInfo一般是事件的信息。
NSLog(@"%@ === %@ === %@", noti.object, noti.userInfo, noti.name);
}
- (void)dealloc{
// 移除当前对象监听的事件
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
B页面
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(goback)];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)goback{
[[NSNotificationCenter defaultCenter]postNotificationName:@"notificationtest" object:@"6666666" userInfo:nil];
[self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end