天天看點

ios 界面傳值

下面我們講講幾種比較常見的界面傳值:正向傳值(屬性傳值),block傳值,代理傳值,單例傳值,通知傳值。

首先 建立一個ViewController ,在建立一個跳轉的控制器 FirstViewController

一、正向傳值

1、首先在 FirstViewController.h 中 聲明一個屬性 (可以是NSString,NSArray,NSDictionary)

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController
@property (nonatomic, copy) NSString * stringData;
@property (nonatomic, strong) NSArray * array;
@property (nonatomic, strong) NSDictionary * dict;
@end
           

2、在 FirstViewController.m 列印傳的值

NSLog(@"正向傳值------%@",self.stringData);
           

3、在ViewController.m 中

UIButton * first = [UIButton buttonWithType:UIButtonTypeCustom];
first.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 100, 120, 200, 50);
[first setTitle:@"正向傳值" forState:UIControlStateNormal];
[first addTarget:self action:@selector(toViewController:) forControlEvents:UIControlEventTouchUpInside];
first.titleLabel.font = [UIFont systemFontOfSize:15];
first.backgroundColor = [UIColor redColor];
[self.view addSubview:first];

- (void)toViewController:(UIButton *)sender {
    FirstViewController * first = [[FirstViewController alloc] init];
    first.stringData = @"正向傳的值";
    [self.navigationController pushViewController:first animated:YES];
}
           

運作

ios 界面傳值

二、block 傳值

1、在FirstViewController.h 中

@property (nonatomic, copy) void(^ myBlock) (NSString *);
           

2、在FirstViewController.m 中 在按鈕的點選事件中,直接調用

self.myBlock(@“block傳的值”);

3、在ViewController.m 中定義一個 UILable * changeLable;用來接受block傳的值

changeLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 380, [UIScreen mainScreen].bounds.size.width, 60)];
    changeLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:changeLable];

   FirstViewController * first = [[FirstViewController alloc] init];
    first.stringData = @"正向傳的值";
    first.myBlock = ^(NSString * string) {
        changeLable.text = string;
    };
    [self.navigationController pushViewController:first animated:YES];
           

運作結果

ios 界面傳值

三、代理

1、在FirstController.h 定義代理

@protocol PushToContentDelegate <NSObject>
- (void)pushToConttent:(id)content;
@end
@property (nonatomic, assign) id<PushToContentDelegate> delegate;
           

2、在FirstController.m中

// 通過代理傳值首次判斷代理是否存在,并在代理能夠響應代理方法時才執行代理方法
if (self.delegate && [self.delegate respondsToSelector:@selector(pushToConttent:)]) {
   [self.delegate pushToConttent:@"代理傳的值"];
}
           

3、在ViewController.m中 , 實作該代理,并實作該代理的方法

@interface ViewController () <PushToContentDelegate>
{
    UILabel * changeLable;
}
@end

- (void)toViewController:(UIButton *)sender {
    FirstViewController * first = [[FirstViewController alloc] init];
    first.stringData = @"正向傳的值";
    first.delegate = self;
    [self.navigationController pushViewController:first animated:YES];
}

- (void)pushToConttent:(id)content {
    changeLable.text = [NSString stringWithFormat:@"%@",content];
}
           

運作效果

ios 界面傳值

四、通知

1、在ViewController.m 中注冊一個通知監聽,并在頁面消失時移除該通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificeM:) name:@"PUSHTOCONTENT" object:nil];

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)notificeM : (NSNotification *)nof {
    NSDictionary * dict = nof.userInfo;
    changeLable.text = [NSString stringWithFormat:@"%@",dict[@"content"]];
}
           

2、在FirstViewController.m 中 發送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@“PUSHTOCONTENT” object:self userInfo:@{@“content”?“通知傳值”}];

運作結果

ios 界面傳值

總結

  • 屬性傳值就是在一個頁面給另一個頁面的屬性指派,通過指派的這個屬性來傳遞的資訊;
  • block傳值和屬性傳值差不多,block代碼塊也是另一個界面的屬性,block将值儲存在代碼塊中,通過頁面的回調代碼塊,以擷取代碼塊傳遞過來的值。
  • 代理方法是用于任意界面之間傳值,隻需要聲明實作代理方法,就可以擷取傳遞過來的值其實
  • 注冊通知與移除通知需要一一對應,同時通知名稱要相同,才能收到該通知發送的消息。

scdn demo下載下傳

GitHub demo下載下傳

繼續閱讀