1、添加UITableView
@property (weak, nonatomic) IBOutlet UITableView *chatDetailView;
2、添加數據
@property (strong, nonatomic) NSArray *:;
_dataArr = @[@"聊天置頂", @"新音訊免打攪", @"聊天室檔案", @"設定以後聊天背景", @"查找聊天內容", @"刪除聊天記載", @"贊揚"];
3、設置代理(沒有設置代理運轉後只能看到一片灰色)
_chatDetailView.delegate = self;
_chatDetailView.dataSource = self; // 這個也可以經過連線
4、完成代理辦法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0 || section == 2) {
return 2;
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static int staticRow = 0;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellID"];
}
cell.textLabel.text = _dataArr[staticRow++];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.section == 4) {
CFComplaintViewController *complaintVC = [[CFComplaintViewController alloc] init];
[complaintVC loadWebviewWithURL:@"http://www.baidu.com"]; // 這裡先用這個url,待更新
UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:complaintVC];
[self presentViewController:naviVC animated:YES completion:nil];
}
}
這裡要拉取出網頁,還得在info.plist添加:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
運轉效果:
進入到贊揚界面背景:點擊“贊揚”,跳轉到贊揚界面
加載網頁1、導入頭文件
#import <WebKit/WebKit.h>
2、添加WKWebview
@property (nonatomic, strong) WKWebView *complaintWebview;
3、添加獲取網頁的辦法
- (void)loadWebviewWithURL:(NSString *)url;
- (void)loadWebviewWithURL:(NSString *)url {
_complaintWebview = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
// 添加進度條(上面會提到)
[_complaintWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self.view addSubview:_complaintWebview];
// 添加代理辦法(上面會提到)
}
添加進度條
1、添加進度條
@property (nonatomic, strong) UIProgressView *loadProgressView;
@property (nonatomic, assign) NSInteger loadCount; // 用於確認能否完全加載:0表示未加載或許加載失敗,1表示加載完成
// 添加進度條
_loadProgressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 5)];
_loadProgressView.trackTintColor = [UIColor blackColor];
_loadProgressView.progressTintColor = [UIColor greenColor];
[_complaintWebview addSubview:_loadProgressView];
// 添加代理辦法
[self.complaintWebview addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
WKWebView *webview = (WKWebView *)object;
if (webview == self.complaintWebview && [keyPath isEqualToString:@"estimatedProgress"]) {
CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
if (newprogress == 1) {
self.loadProgressView.hidden = YES;
[self.loadProgressView setProgress:0 animated:NO];
}else {
self.loadProgressView.hidden = NO;
[self.loadProgressView setProgress:newprogress animated:YES];
}
}
}
- (void)setLoadCount:(NSInteger)loadCount {
_loadCount = loadCount;
if (loadCount == 0) {
self.loadProgressView.hidden = YES;
[self.loadProgressView setProgress:0 animated:NO];
}else {
self.loadProgressView.hidden = NO;
CGFloat oldP = self.loadProgressView.progress;
CGFloat newP = (1.0 - oldP) / (loadCount + 1) + oldP;
if (newP > 0.95) {
newP = 0.95;
}
[self.loadProgressView setProgress:newP animated:YES];
}
}
// 頁面開端加載時調用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
self.loadCount ++;
}
// 內容前往時
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
self.loadCount --;
}
// 加載失敗
- (void)webView:(WKWebView *)webView didFailNavigation: (null_unspecified WKNavigation *)navigation withError:(NSError *)error {
self.loadCount --;
NSLog(@"%@",error);
}
// 取消監聽
- (void)dealloc {
[_complaintWebview removeObserver:self forKeyPath:@"estimatedProgress"];
}
與h5停止交互
背景:當贊揚界面是h5寫的,如何在用戶點擊“提交贊揚”按鈕時前往上個界面
WKWebview(該局部待“與h5停止交互”功用添加再更新詳細內容) KVO用法1、添加察看者
2、在察看者中完成監聽辦法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {}
3、移除察看者
[與h5停止交互,待下次更新,如有誤,請幫助指出,謝謝]
demo的github地址:https://github.com/Yangchengfeng/LMN_WKWebViewDemo
【仿微信小功用之“贊揚”】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!