今天看了一下ios 頁面傳值的方式大致分為四種:
代理delegateblock通知單例class今天試了一下前三種,在這裡記錄一下
下面示例是有兩個頁面,每個頁面都有一個按鈕Button,點擊第一個頁面的按鈕回調到第二個頁面,再點擊第二個頁面回跳轉道第一個頁面,第一個按鈕的標題變為第二個按鈕傳回的值。
代理delegate
代理似乎是我的心結,能用API 但是就是不會自己寫,這也是今天會寫傳值的原因。
假設兩個頁面傳值,協議類應該寫在哪,代理應該定義在那個頁面?
總結的時候我覺得可以這樣來想:第二個頁面想要修改第一個頁面的值,但是他沒辦法做到,所以他需要一個代理來幫助它修改,所以代理需要定義在第二個頁面。第一個頁面需要支持代理。
第一個頁面:
@interface frontVC : UIViewController@property (weak, nonatomic) IBOutlet UIButton *frontBtn; @end
#import frontVC.h
@interface frontVC ()
{
realendVC *realendvc;//第二個頁面
}
@end
@implementation frontVC
- (void)viewDidLoad {
[super viewDidLoad];
//從storyboard中獲得第二個頁面
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@Main bundle:[NSBundle mainBundle]];
realendvc = [storyboard instantiateViewControllerWithIdentifier:@second];
//設置代理
realendvc.delegate = self;
}
//按鈕點擊事件
- (IBAction)click:(id)sender {
[self.navigationController pushViewController:realendvc animated:YES];
}
//代理方法
- (void)changebutton:(NSString *)string
{
[_frontBtn setTitle:string forState:UIControlStateNormal];
}
@end
@protocol change- (void) changebutton:(NSString *)string; @end @interface realendVC : UIViewController @property (weak, nonatomic) IBOutlet UIButton *realendBtn; @property (nonatomic,weak) id delegate; @end
#import realendVC.h
@interface realendVC ()
@end
@implementation realendVC
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)click:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
if (self.delegate) {
[self.delegate changebutton:@成功];
}
}
@end
block
第一個頁面
#import#import realendVC.h @interface frontVC : UIViewController @property (weak, nonatomic) IBOutlet UIButton *frontBtn; @end
#import frontVC.h
@interface frontVC ()
{
realendVC *realendvc;//第二個頁面
}
@end
@implementation frontVC
- (void)viewDidLoad {
[super viewDidLoad];
//從storyboard中獲得第二個頁面
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@Main bundle:[NSBundle mainBundle]];
realendvc = [storyboard instantiateViewControllerWithIdentifier:@second];
//在block中允許訪問變量,但不允許修改,需要加上下面一句代碼
__block UIButton * button = _frontBtn;
realendvc.myblock = ^(NSString *string)
{
[button setTitle:string forState:UIControlStateNormal];
};
}
//按鈕點擊事件
- (IBAction)click:(id)sender {
[self.navigationController pushViewController:realendvc animated:YES];
}
@end
#importtypedef void(^Myblock) (NSString *); @interface realendVC : UIViewController @property (weak, nonatomic) IBOutlet UIButton *realendBtn; @property (copy,nonatomic) Myblock myblock; @end
#import realendVC.h
@interface realendVC ()
@end
@implementation realendVC
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)click:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
self.myblock(@成功);
}
@end
第一個頁面
#import#import realendVC.h @interface frontVC : UIViewController @property (weak, nonatomic) IBOutlet UIButton *frontBtn; @end
#import frontVC.h
@interface frontVC ()
{
realendVC *realendvc;//第二個頁面
}
@end
@implementation frontVC
- (void)viewDidLoad {
[super viewDidLoad];
//從storyboard中獲得第二個頁面
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@Main bundle:[NSBundle mainBundle]];
realendvc = [storyboard instantiateViewControllerWithIdentifier:@second];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(change:) name:@buttonobject:nil];
}
//通知調用事件
- (void)change:(NSNotification *)notification
{
[_frontBtn setTitle:(NSString *)[notification object] forState:UIControlStateNormal];
}
//按鈕點擊事件
- (IBAction)click:(id)sender {
[self.navigationController pushViewController:realendvc animated:YES];
}
@end
#import@interface realendVC : UIViewController @property (weak, nonatomic) IBOutlet UIButton *realendBtn; @end
#import realendVC.h
@interface realendVC ()
@end
@implementation realendVC
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)click:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:@button object:@成功];
}
@end