由于最近較忙好久沒(méi)寫(xiě)了,最近看了一種設(shè)計(jì)模式,"self-manager" 感覺(jué)特別爽,跟大多數(shù)人一樣之前想把跳轉(zhuǎn)寫(xiě)在view里但是沒(méi)有找到辦法,所以要回代理啊,block 傳值, 學(xué)習(xí)了這種設(shè)計(jì)模式之后,沒(méi)找到demo, 雖然看了sunnyxx 大神寫(xiě)的博客,但是我們要用起來(lái)的,所以寫(xiě)了一個(gè)小demo
sunnyxx 介紹的已經(jīng)很詳細(xì)了,所以 直接轉(zhuǎn)載了,在下面附上Demo
它的職責(zé)包括:
- 通過(guò)傳入的 URL鹏溯,加載并展示頭像圖片
- 顯示一些附屬信息胡本,比如大V的標(biāo)志
- 將用戶點(diǎn)擊頭像的事件傳遞給外層的 View Controller 跳轉(zhuǎn)到用戶信息頁(yè)面
于是乎這個(gè) Widget 的 API 可以長(zhǎng)這個(gè)樣子:
@interface XHAvatarView : UIView
- (void)configureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info tapped:(void (^)())block;
@end
@interface XHAvatarView (XHAvatarViewManager)
- (void)selfManagedConfigureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info uid: (NSString \*)uid;
@end
#import "TempViewController.h"
@interface XHAvatarView ()
@property (nonatomic,copy) void (^tempBlcok)() ;
@end
@implementation XHAvatarView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10, 10, 50, 50);
btn.backgroundColor = [UIColor redColor];
[btn addTarget:self action:@selector(btndown) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn];
}
return self;
}
- (void)btndown{
if (self.tempBlcok) {
self.tempBlcok();
}
}
- (void)configureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info tapped:(void (^)())block{
self.tempBlcok = block;
// 賦值操作.
}
@end
@implementation XHAvatarView (XHAvatarViewManager)
- (void)selfManagedConfigureWithAvatarURL:(NSURL \*)URL VIPInfo:(id)info uid:(NSString \*)uid{
[self configureWithAvatarURL:URL VIPInfo:info tapped:^{
UINavigationController \*navigationController= (id)UIApplication.sharedApplication.delegate.window.rootViewController;
NSLog(@"頁(yè)面跳轉(zhuǎn)了====");
TempViewController \* vc = [[TempViewController alloc] init];
[navigationController pushViewController:vc animated:YES];
}];
}
原博客中提到 "實(shí)現(xiàn)時(shí)最好要調(diào)用 View 主類(lèi)提供的 API:"
如果不是調(diào)用主View 的話會(huì)有什么問(wèn)題,暫時(shí)不清楚, 還有就是調(diào)用主View 的時(shí)候.uid 怎么傳進(jìn)來(lái).
源碼參考