界面之間的數(shù)據(jù)正逆向/多層次傳遞方法

1 初始化傳值

(重寫init方法,讓后面攜帶需要傳入的數(shù)據(jù),然后在對界面初始化,使用這種方法必須要在初始化前就已經有數(shù)據(jù)了比較適合)

[objc]?view plain?copy

#import???


@interface?CustomView?:?UIView??

//?需要注意必須在?(.h)對方法進行聲明?在外部創(chuàng)建這個類的時候才能看到??

-?(instancetype)initWithFrame:(CGRect)frame?withInformation:(NSDictionary?*)dict;??

@end??

[objc]?view plain?copy

#import?"CustomView.h"??


@implementation?CustomView??


-?(instancetype)initWithFrame:(CGRect)frame?withInformation:(NSDictionary?*)dict??

{??

self?=?[super?initWithFrame:frame];??

if?(self)?{??

//?進行頁面配置,直接可以拿到自己想要的數(shù)據(jù)通過字典??

????}??

return?self;??

}??


@end??

[objc]?view plain?copy

-?(void)initUserInterface??

{??

NSDictionary?*dict?=?[NSDictionary?dictionary];??

//?調用init自定義方法?傳入數(shù)據(jù)??

CustomView?*customView?=?[[CustomView?alloc]initWithFrame:self.view.bounds?withInformation:dict];??

[self.view?addSubview:customView];??

}??

2 屬性傳值

?(屬性傳值比較合適界面是先初始化的,而數(shù)據(jù)在界面初始化之后才拿到的,當想要對界面的元素的內容進行更新,直接通過屬性拿到對應的元素進行更改比較方便。特別是做數(shù)據(jù)請求用tableView來展示的時候,請求數(shù)據(jù)和頁面創(chuàng)建同步進行,所以用init傳請求數(shù)據(jù)必然會是空,那么就需要在數(shù)據(jù)請求成功用,在把界面元素就行刷新,而cell也是自定義的,最好把需要更新的控件定義成屬性就好對數(shù)據(jù)進行刷新處理了)

[objc]?view plain?copy

#import???



@interface?CustomView?:?UIView??


@property?(nonatomic,retain)UILabel?*label;??


@end??

[objc]?view plain?copy

#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??

[objc]?view plain?copy

#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?=?@"修改顯示數(shù)據(jù)";??

[self.view?addSubview:customView];??

}??


@end??

3 方法參數(shù)傳值

?(方法參數(shù)傳值也很適合對界面元素更新使用,當想要讓封裝好的一個TableView刷新請求回來數(shù)據(jù)時,通過調用封裝的這個方法傳入數(shù)據(jù)就可以刷新界面數(shù)據(jù))

4 協(xié)議傳值

(比較適合兩個界面直接的逆向傳值__>也是類似實現(xiàn)系統(tǒng)的代理方法,當某個封裝好的類,定義一個自己的代理,當這個類里觸發(fā)的某個事件需要把數(shù)據(jù)傳出去,就在協(xié)議里定義一個方法,當遵守這個協(xié)議的實例調用這個方法就可以訪問后面攜帶的參數(shù))

以下是簡單封裝的一個view來做例子說明 ?分別是封裝的.h ?.m ?文件

[objc]?view plain?copy

#import???


@protocol?CustomViewDelegate???

//?delegate?必須實現(xiàn)的方法??

@required??

-?(void)sendInformation:(NSInteger)tag;??

//?delegate?選擇實現(xiàn)的方法??

@optional??


@end??


@interface?CustomView?:?UIView??


@property?(nonatomic,assign)id??delegate;??


@end??

[objc]?view plain?copy

#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

[objc]?view plain?copy

#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];??

//?創(chuàng)建實例給他代理??

customView.delegate?=?self;??

[self.view?addSubview:customView];??

}??


#pragma?mark?-?CustomViewDelegate??

-?(void)sendInformation:(NSInteger)tag??

{??

//?當觸發(fā)button事件的時候,就會調用這個方法,把數(shù)據(jù)傳過來,類比于tableView?點擊的了對應的行就會走代理的方法??

NSLog(@"%ld",tag);??

}??


-?(void)didReceiveMemoryWarning?{??

[super?didReceiveMemoryWarning];??

//?Dispose?of?any?resources?that?can?be?recreated.??

}??


@end??

5 Block傳值


6 單例傳值

?(使用單例一般比較時候存儲用戶信息之類的,方便數(shù)據(jù)訪問或其他時候數(shù)據(jù)隨時調用)

7 通知傳值

?(當需要夸多層次的頁面進行數(shù)據(jù)傳送的時候,注冊通知來實現(xiàn)是比較方便的,而且使用起來也是非常簡單)

[objc]?view plain?copy

#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??

//?發(fā)送通知后,就會走這個方法??

-?(void)getInformation:(NSNotification?*)noti??

{??


/****************???Notifications???****************/??

/*

????@interface?NSNotification?:?NSObject?


????@property?(readonly,?copy)?NSString?*name;

????@property?(readonly,?retain)?id?object;

????@property?(readonly,?copy)?NSDictionary?*userInfo;


????*/??

//?把傳過來的數(shù)據(jù)進行打印??


NSLog(@"%@",noti.object);?//?直接用點屬性獲取傳送過來的數(shù)據(jù)即可??


}??


@end??

[objc]?view plain?copy

#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?發(fā)送通知??

-?(void)postNoti??

{??

NSString?*string?=?@"send?any?data";??

[[NSNotificationCenter?defaultCenter]?postNotificationName:@"sendData"??

?object:string];//?object?為ID?可以傳送任意類型數(shù)據(jù)?這里傳得時字符串??

}??


@end??

8 extern全局變量

9 數(shù)據(jù)存儲

(數(shù)據(jù)持久化 寫入沙盒)?

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容