您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 介面之間的資料正向、逆向與跨層傳遞方法

編輯:iOS 開發綜合

在 iOS 開發中,畫面之間傳值可說是家常便飯。依「資料在何時取得」以及「要傳往哪個方向」,可以拆成好幾種做法。本文整理九種常見方式,並說明各自適用的情境。

1. 初始化傳值

做法是覆寫 init 方法,讓它帶入所需的資料,再完成介面初始化。這種方式的前提是:初始化之前資料就已經備妥,因此比較適合資料先到的情況。

#import <UIKit/UIKit.h>

@interface CustomView : UIView
// 必須在 .h 中宣告方法,外部建立這個類別時才看得到
- (instancetype)initWithFrame:(CGRect)frame withInformation:(NSDictionary *)dict;
@end
#import "CustomView.h"

@implementation CustomView
- (instancetype)initWithFrame:(CGRect)frame withInformation:(NSDictionary *)dict {
    self = [super initWithFrame:frame];
    if (self) {
        // 進行頁面配置,直接拿到自己想要的資料
    }
    return self;
}
@end
- (void)initUserInterface {
    NSDictionary *dict = [NSDictionary dictionary];
    // 呼叫 init 自訂方法並傳入資料
    CustomView *customView = [[CustomView alloc]initWithFrame:self.view.bounds withInformation:dict];
    [self.view addSubview:customView];
}

2. 屬性傳值

屬性傳值適合「介面先初始化、資料後到」的情況。當想更新介面元素內容時,直接透過屬性取得元素再改就好。特別是搭配 tableView 做網路請求時,請求與頁面建立是同步進行的,用 init 傳資料必然是空值,所以得在請求成功後再刷新介面;而 cell 若也是自訂的,最好把要更新的控件宣告成屬性,方便刷新。

#import <UIKit/UIKit.h>

@interface CustomView : UIView
@property (nonatomic,retain)UILabel *label;
@end
#import "CustomView.h"

@implementation CustomView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _label = [[UILabel alloc]init];
        _label.text = @"屬性傳值";
        [self addSubview:_label];
    }
    return self;
}
@end
#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)initUserInterface {
    CustomView *customView = [[CustomView alloc]initWithFrame:self.view.bounds];
    // 拿到它的屬性進行操作
    customView.label.text = @"修改顯示資料";
    [self.view addSubview:customView];
}
@end

3. 方法參數傳值

方法參數傳值同樣很適合更新介面元素。當你想讓一個已封裝好的 TableView 用回來的資料刷新時,只要呼叫它對外暴露的方法並把資料傳進去即可。

4. 協議傳值

協議適合兩個介面之間的「逆向傳值」,概念近似系統的代理方法:當某個封裝好的類別定義自己的代理,一旦類別內部觸發某事件需要把資料往外送,就在協議中宣告一個方法;遵守該協議的實例呼叫這個方法,就能取用後續攜帶的參數。

以下用一個簡單封裝的 view 來說明,分別是 .h 與 .m:

#import <UIKit/UIKit.h>

@protocol CustomViewDelegate <NSObject>
// delegate 必須實作的方法
@required
- (void)sendInformation:(NSInteger)tag;
// delegate 選擇性實作的方法
@optional
@end

@interface CustomView : UIView
@property (nonatomic,assign)id <CustomViewDelegate> delegate;
@end
#import "CustomView.h"

@implementation CustomView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tag = 111;
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    return self;
}
- (void)buttonPressed:(UIButton *)sender {
    [self.delegate sendInformation:sender.tag];
}
@end

主畫面的 ViewController:

#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()<CustomViewDelegate>
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)initUserInterface {
    CustomView *customView = [[CustomView alloc]initWithFrame:self.view.bounds];
    // 建立實例並指定代理
    customView.delegate = self;
    [self.view addSubview:customView];
}
#pragma mark - CustomViewDelegate
- (void)sendInformation:(NSInteger)tag {
    // 觸發 button 事件時就會呼叫這個方法,把資料傳過來
    // 概念近似 tableView 點擊某行會走代理方法
    NSLog(@"%ld",tag);
}
@end

5. Block 傳值

利用 block 作為回呼,讓被呼叫方在事件發生時把結果交回呼叫方,寫法比代理更精簡,適合同一檔案內的單向回傳。

6. 單例傳值

單例通常用來存放使用者資訊一類需要全域存取的資料,方便隨時調用。

7. 通知傳值

當需要跨越多層頁面傳遞資料時,註冊通知相當方便,使用起來也很直覺。

#import "ViewController.h"
#import "CustomView.h"

@interface ViewController ()
- (void)getInformation:(NSNotification *)noti;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUserInterface];
}
- (void)initUserInterface {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getInformation:) name:@"sendData" object:nil];
}
#pragma mark - NSNotificationCenter methods
// 發送通知後,就會走這個方法
- (void)getInformation:(NSNotification *)noti {
    /* NSNotification 介面
       @property (readonly, copy) NSString *name;
       @property (readonly, retain) id object;
       @property (readonly, copy) NSDictionary *userInfo;
    */
    // 直接透過點語法取得傳送過來的資料
    NSLog(@"%@",noti.object);
}
@end
#import "CustomView.h"

@implementation CustomView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button addTarget:self action:@selector(postNoti) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    return self;
}
// 點擊 button 後發送通知
- (void)postNoti {
    NSString *string = @"send any data";
    // object 型別為 id,可傳送任意型別資料,這裡傳字串
    [[NSNotificationCenter defaultCenter] postNotificationName:@"sendData" object:string];
}
@end

8. extern 全域變數

以 extern 宣告全域變數,讓多個檔案共用同一份狀態,適合少量、簡單的共享資料。

9. 資料儲存

把資料持久化寫入沙盒(Sandbox),讓資料跨啟動週期都能保留,適合需要長期保存的內容。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved