iOS傳值
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
相關文章
- iOS Block傳值、代理傳值、通知中心iOSBloC
- iOS block 反向傳值iOSBloC
- iOS 常用傳值方式iOS
- IOS學習筆記(頁面傳值:屬性傳值,協議代理傳值,閉包傳值)iOS筆記協議
- iOS開發之利用Block逆向傳值iOSBloC
- 淺談iOS常用的幾種傳值方式iOS
- iOS開發之通過代理逆向傳值iOS
- iOS——使用StroryBoard頁面跳轉及傳值iOS
- iOS的Target-Action模式傳值的最佳方式iOS模式
- vue父子傳值與非父子傳值Vue
- Java傳參傳值Java
- 元件:非父子間傳值(同級傳值)元件
- Vue--子元件互相傳值,子元件來回傳值,傳值反覆橫跳Vue元件
- 在html中使用axios傳送請求到servlet時遇到的傳值問題HTMLiOSServlet
- Android 元件系列-----Activity的傳值和回傳值Android元件
- 值傳遞與引用傳遞
- 值傳遞和引用傳遞
- Vue 元件傳值Vue元件
- java值傳遞Java
- 按值傳遞
- Intent傳值與Bundle傳值的區別(原始碼分析)Intent原始碼
- Vue 路由傳值(傳參)詳解Vue路由
- 網頁間傳值怎麼傳網頁
- JAVA值傳參和引用傳參Java
- Mybatis中List傳值MyBatis
- Vue 元件間傳值Vue元件
- Java只有值傳遞Java
- Fragment傳值到ActivityFragment
- javasript 按值傳遞Java
- swift 閉包傳值Swift
- block,代理,通知傳值BloC
- Razor傳值到jsJS
- PHP後臺傳值PHP
- JavaScript的值傳遞和引用傳遞JavaScript
- 快速搞懂值傳遞與引用傳遞
- C語言的傳值與傳地址C語言
- Java 是傳值還是傳引用 (轉)Java
- Java的值傳遞和引用傳遞Java