标签导航
标签导航有一定的指导原则:标签栏位于屏幕下方,占有49点的屏幕空间,有事可以隐藏起来;为了点击方便,标签栏中的标签不能超过5个,如果超过5个,则最后的一个显示为“更多”,点击“更多”标签会出现更多的列表。
标签导航模式的各个标签分别代表一个功能模块,各个功能模块之间相对独立。
可以使用工程模板Tabbed Application创建标签导航模式的应用,现有两个场景(Scene),可添加或修改场景。添加场景:拖拽有个View controller到设计界面,按住control键从 Tab Bar Controller Sence拖拽到View controller,从弹出菜单中选择View controllers
树形结构导航
将导航控制器(UINavigationController)与表现结合使用,主要用于构建从属关系的导航。这种导航模式采用分层组织信息的方式,可以帮我们构建iOS效率型应用程序:相册应用,iPod Touch自带的邮件应用。可使用工程模板Master-Detail Application.
运行效果图
数据plist文件:
视图的树形层级:
类图结构:
实例代码:
1.ViewController : UIViewController
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSDictionary *dictData;
@property (strong, nonatomic) NSArray *listData;
@end
#import "ViewController.h"
#import "CitiesViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"plist" ofType:@"plist"];
self.dictData = [[NSDictionary alloc]initWithContentsOfFile:path];
self.listData = [self.dictData allKeys];
self.title = @"城市信息";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [self.listData objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger row = [indexPath row];
CitiesViewController *citiesViewController = [[CitiesViewController alloc]initWithStyle:UITableViewStylePlain];
NSString *selectName = [self.listData objectAtIndex:row];
citiesViewController.listData = [self.dictData objectForKey:selectName];
citiesViewController.title = selectName;
[self.navigationController pushViewController:citiesViewController animated:YES];
}
@end
2.CitiesViewController : UITableViewController
@interface CitiesViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
@property(strong,nonatomic)NSDictionary *listData;
@end
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSInteger row = [indexPath row];
NSString *itemName = [[self.listData allKeys] objectAtIndex:row];
NSDictionary *dict = [self.listData objectForKey:itemName];
cell.textLabel.text = [dict objectForKey:@"name"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
NSInteger row = [indexPath row];
NSString *itemName = [[self.listData allKeys] objectAtIndex:row];
NSDictionary *dict = [self.listData objectForKey:itemName];
detailViewController.url = [dict objectForKey:@"url"];
NSString *name = [dict objectForKey:@"name"];
detailViewController.title = name;
[self.navigationController pushViewController:detailViewController animated:YES];
}
3.DetailViewController : UIViewController
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property(strong,nonatomic)NSString *url;
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.webView.delegate = self;
NSURL *url = [NSURL URLWithString:self.url];
NSURLRequest *requst = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requst];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"Finish");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"%@",[error description]);
}
@end
注意:我们选择的工程模板不是导航控制器模板,所以需要自己添加导航控制器并将其作为根视图控制器。选中View Controller,然后打开菜单Editor -> Embed In ->Navigation Controller