近來項目告一段落,閑來無事,想想整理之前的筆記也開始向部落格上貢獻一點資源,供後來者借鑒,也可以當做永久的筆記,以後有時間會慢慢的将所有寫過的東西上傳到block上,
界面間傳值的方式有很多種
- 從前往後傳值的可以用屬性傳值比較簡單,如果你說還有沒有别的方式傳值,我隻能說我沒有用過别的,最多用過通知傳值,其他的方式還沒有試過,由于屬性傳值比較簡單,這裡就不在獒述了
- 界面從後往前傳值 ,主要介紹block傳值,代理傳值,和通知傳值
Block傳值
我這裡是履歷兩個控制器 A ,B ViewController ,PassViewcontroller
//
// ViewController.h
// BlockPassValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// BlockPassValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import "ViewController.h"
#import "PassViewController.h"
@interface ViewController ()
@property (nonatomic ,strong)UILabel *labe;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_labe = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
_labe.backgroundColor = [UIColor cyanColor];
[self.view addSubview:_labe];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
PassViewController *pass = [PassViewController new];
//展開指派
[pass PassValeText:^(NSString *Value) {
_labe.text = Value;
}];
[self.navigationController presentViewController:pass animated:YES completion:nil];
}
//
// PassViewController.h
// BlockPassValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void (^Valueblock)(NSString *Value);
@interface PassViewController : UIViewController
@property (nonatomic ,copy)Valueblock String;
- (void)PassValeText:(Valueblock)block;
//
// PassViewController.m
// BlockPassValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import "PassViewController.h"
#import "ViewController.h"
@interface PassViewController ()
@property (nonatomic ,strong)UITextField *text;
@end
@implementation PassViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_text = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
_text.layer.borderColor = [UIColor blackColor].CGColor;
_text.layer.borderWidth = ;
[self.view addSubview:_text];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(, , , );
[button setTitle:@"傳值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
- (void)PassValeText:(Valueblock)block
{
self.String = block;
}
- (void)ButtonAction:(UIButton *)button
{
if (self.String != nil) {
self.String(_text.text);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
代理傳值
同樣是建立兩個控制器 A,B ViewController ,ProtocolViewController 傳值 B->A
//
// ViewController.h
// DelegateResource
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
// ViewController.m
// DelegateResource
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import "ViewController.h"
#import "ProtocolViewController.h"
@interface ViewController ()<PassingValueDelegate>
@property (nonatomic ,strong)UILabel *labe;
@property (nonatomic ,strong)NSString *string;
@end
@implementation ViewController
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor whiteColor];
_labe = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
_labe.backgroundColor = [UIColor cyanColor];
// _labe.text = @"2344";
// self.labe = label;
[self.view addSubview:_labe];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
ProtocolViewController *pro = [ProtocolViewController new];
_labe.text = pro.text;
pro.delegate = self;
UINavigationController *mna = [[UINavigationController alloc]initWithRootViewController:pro];
[self.navigationController presentViewController:mna animated:YES completion:nil];
}
- (void)showValueToDelegate:(id)text
{
_labe.text =text;
}
//
// ProtocolViewController.h
// DelegateResource
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import <UIKit/UIKit.h>
@class ProtocolViewController;
@protocol PassingValueDelegate<NSObject>
@optional
- (void)viewController:(ProtocolViewController *)protocol didPassingValueWithInfo:(id)info;
- (void)showValueToDelegate:(id)text;
@required
@end;
@interface ProtocolViewController : UIViewController
@property (nonatomic ,strong)NSString *text;
@property (nonatomic ,assign)id <PassingValueDelegate>delegate;
@end
// ProtocolViewController.m
// DelegateResource
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import "ProtocolViewController.h"
#import "ViewController.h"
@interface ProtocolViewController ()
@property (nonatomic ,strong)UITextField *textString;
@end
@implementation ProtocolViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
_textString = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
_textString.layer.borderColor = [UIColor blackColor].CGColor;
_textString.layer.borderWidth = ;
[self.view addSubview:_textString];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(, , , );
[button addTarget:self action:@selector(ButtongAction:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"跳轉" forState:UIControlStateNormal];
[self.view addSubview:button];
}
- (void)ButtongAction:(UIButton *)button
{
[self.delegate showValueToDelegate:_textString.text];
ViewController *view = [ViewController new];
[self dismissViewControllerAnimated:YES completion:nil];
}
通知傳值
建立兩個控制器 A,B ViewController ,NoticeViewController
傳值B->A
//
// ViewController.h
// NotificationToValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// NotificationToValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import "ViewController.h"
#import "NoticeViewController.h"
@interface ViewController ()
@property (nonatomic ,strong)UILabel *labe;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_labe = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
_labe.backgroundColor = [UIColor cyanColor];
[self.view addSubview:_labe];
//注冊通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notice:) name:@"PostValue" object:nil];
}
- (void)notice:(NSNotification *)notice
{
//接受通知傳值并指派
NSString *string = [notice.userInfo valueForKey:@"value"];
_labe.text = string;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NoticeViewController *noticeVC = [NoticeViewController new];
[self.navigationController presentViewController:noticeVC animated:YES completion:nil];
}
// NoticeViewController.h
// NotificationToValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import <UIKit/UIKit.h>
@interface NoticeViewController : UIViewController
@end
// NoticeViewController.m
// NotificationToValue
//
// Created by [email protected] on 14/7/8.
// Copyright © 2014年 [email protected] All rights reserved.
//
#import "NoticeViewController.h"
@interface NoticeViewController ()
@property (nonatomic ,strong)UITextField *text;
@end
@implementation NoticeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_text = [[UITextField alloc]initWithFrame:CGRectMake(, , , )];
_text.layer.borderColor = [UIColor blackColor].CGColor;
_text.layer.borderWidth = ;
[self.view addSubview:_text];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(, , , );
[button setTitle:@"傳值" forState:UIControlStateNormal];
[button addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:button];
}
- (void)ButtonAction:(UIButton *)button
{
//建立通知
/*
*通知發送的都是一個字典
*傳送值包裝成字典
*/
NSDictionary *dicy = @{@"value":_text.text};
[[NSNotificationCenter defaultCenter]postNotificationName:@"PostValue" object:nil userInfo:dicy];
[self dismissViewControllerAnimated:YES completion:nil];
}
上面就是全部的詳細代碼,