(重寫init方法,讓後面攜帶需要傳入的數據,然後在對界面初始化,使用這種方法必須要在初始化前就已經有數據了比較適合)
#import@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];
}(屬性傳值比較合適界面是先初始化的,而數據在界面初始化之後才拿到的,當想要對界面的元素的內容進行更新,直接通過屬性拿到對應的元素進行更改比較方便。特別是做數據請求用tableView來展示的時候,請求數據和頁面創建同步進行,所以用init傳請求數據必然會是空,那麼就需要在數據請求成功用,在把界面元素就行刷新,而cell也是自定義的,最好把需要更新的控件定義成屬性就好對數據進行刷新處理了)
#import@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(方法參數傳值也很適合對界面元素更新使用,當想要讓封裝好的一個TableView刷新請求回來數據時,通過調用封裝的這個方法傳入數據就可以刷新界面數據)
(比較適合兩個界面直接的逆向傳值__>也是類似實現系統的代理方法,當某個封裝好的類,定義一個自己的代理,當這個類裡觸發的某個事件需要把數據傳出去,就在協議裡定義一個方法,當遵守這個協議的實例調用這個方法就可以訪問後面攜帶的參數)
以下是簡單封裝的一個view來做例子說明 分別是封裝的.h .m 文件
#import@protocol CustomViewDelegate // delegate 必須實現的方法 @required - (void)sendInformation:(NSInteger)tag; // delegate 選擇實現的方法 @optional @end @interface CustomView : UIView @property (nonatomic,assign)id 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#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.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
(使用單例一般比較時候存儲用戶信息之類的,方便數據訪問或其他時候數據隨時調用)
(當需要誇多層次的頁面進行數據傳送的時候,注冊通知來實現是比較方便的,而且使用起來也是非常簡單)
#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
@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
(數據持久化 寫入沙盒)