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
首頁面顯示VC
#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);}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
5 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{ /****************Notifications****************/ /* @interface NSNotification : NSObject <NSCopying, NSCoding> @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"; [[NSNotificationCenter defaultCenter] postNotificationName:@"sendData" object:string];// object 為ID 可以傳送任意類型資料 這裡傳得時字串}@end
8 extern全域變數
9 資料存放區
(資料持久化 寫入沙箱)