声明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨
/* 思路
1 左边是一个VC,加入tableView作为标签,右边也是一个VC,加入WebView显示要加载的内容
2 在设置监听中心 监听左边 ,左边的cell选中时发送通知,右边接到消息即解析url到webView 具体参考下面代码
注意
操作的时候记得把屏幕横向过来 */
左边
#import "ViewController.h"
NSString *const WebURL = @"WebURL";
NSString *const WebURLNSNotificationCenter = @"WebURLNSNotificationCenter";
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
NSDictionary *dataDic;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
dataDic = [NSDictionary dictionaryWithObjectsAndKeys:@"http://www.baidu.com",@"百度",@"http://www.qq.com",@"腾讯",@"http://www.163.com",@"网易", nil];
UITableView *useTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
useTable.delegate = self;
useTable.dataSource =self;
[self.view addSubview:useTable];
}
#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return dataDic.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell ==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
NSLog(@"%@",dataDic);
NSArray *AllKey = [dataDic allKeys];
NSArray *AllValue = [dataDic allValues];
cell.textLabel.text = AllKey[indexPath.row];
cell.detailTextLabel.text = AllValue[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
NSArray *values = [dataDic allValues];
NSArray *mark = values[indexPath.row];
NSDictionary *userinfo = @{WebURL:mark};
[center postNotificationName:WebURLNSNotificationCenter object:nil userInfo:userinfo];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
右边
#import "RootViewController.h"
@interface RootViewController ()
{
UIWebView *webView;
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.translucent = NO;
webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
[webView loadRequest:request];
webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
// 监听左边视图控制器发送的消息
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(changeURL:) name:@"WebURLNSNotificationCenter" object:nil];
[self.view addSubview:webView];
// Do any additional setup after loading the view.
}
-(void)changeURL:(NSNotification*)center
{
NSLog(@"%@",center.userInfo);
NSString *url = center.userInfo[@"WebURL"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end