知乎日报-ThemePage页_WKWebViewController
包含文件
- DailyThemesViewModel.h, DailyThemesViewModel.m
- DailyThemesViewController.h, DailyThemesViewController.m
- WKWebViewController.h, WKWebViewController.m
1、数据获取
- (instancetype)initWithViewModel:(StoryContentViewModel *)vm {
self = [super init];
if (self) {
self.viewmodel = vm;
[_viewmodel addObserver:self forKeyPath:@"storyModel" options:NSKeyValueObservingOptionNew context:nil];
[_viewmodel getStoryContentWithStoryID:_viewmodel.loadedStoryID];
}
return self;
}
2、更新子控件
- (void)initSubViews {
WKWebView *wk = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-43)];
[self.view addSubview:wk];
_webView = wk;
UIView *navBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
navBar.backgroundColor = [UIColor whiteColor];
[self.view addSubview:navBar];
UIView *toolBar = [[UIView alloc] initWithFrame:CGRectMake(0.f, kScreenHeight-43, kScreenWidth, 43)];
UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth/5, 43)];
[backBtn setImage:[UIImage imageNamed:@"News_Navigation_Arrow"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:backBtn];
UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth/5,0 ,kScreenWidth/5 , 43)];
[nextBtn setImage:[UIImage imageNamed:@"News_Navigation_Next"] forState:UIControlStateNormal];
[nextBtn addTarget:self action:@selector(nextStoryAction:) forControlEvents:UIControlEventTouchUpInside];
[toolBar addSubview:nextBtn];
UIButton *votedBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth/5)*2, 0 ,kScreenWidth/5 , 43)];
[votedBtn setImage:[UIImage imageNamed:@"News_Navigation_Voted"] forState:UIControlStateNormal];
[toolBar addSubview:votedBtn];
UIButton *sharedBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth/5)*3, 0 ,kScreenWidth/5 , 43)];
[sharedBtn setImage:[UIImage imageNamed:@"News_Navigation_Share"] forState:UIControlStateNormal];
[toolBar addSubview:sharedBtn];
UIButton *commentdBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth/5)*4, 0 ,kScreenWidth/5 , 43)];
[commentdBtn setImage:[UIImage imageNamed:@"News_Navigation_Comment"] forState:UIControlStateNormal];
[toolBar addSubview:commentdBtn];
[self.view addSubview:toolBar];
}
3、点击事件
- (void)backAction:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)nextStoryAction:(id)sender {
[_viewmodel getNextStoryContent];
}
4、监听事件,更新UI
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"storyModel"]) {
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_viewmodel.share_URL]]];
}
}