ios-使用树形结构导航模式

来源:互联网 发布:农产品追溯系统源码 编辑:程序博客网 时间:2024/06/05 09:24

// 树形结构导航模式

/*

 树形结构导航模式也是非常重要的导航模式,它将导航视图控制器(UINavigationController)与表视图(UITableView)结合使用,主要用于构建有从属关系的导航。这种导航模式采用分层组织信息的方式,可以帮助我们构建ios效率型应用程序

 */

代码实现如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];    self.window.backgroundColor = [UIColor grayColor];    [self.window makeKeyAndVisible];        KFViewController *viewCtl = [[KFViewController alloc] init];    self.navigationController = [[UINavigationController alloc] initWithRootViewController:viewCtl];    self.window.rootViewController = self.navigationController;    [self.navigationController release];    [viewCtl release];    return YES;        // 树形结构导航    /*     树形结构导航模型中,有两个根视图控制器:     一个是应用程序的根视图控制器,它是UINavigationController的实例,通过self.window.rootViewController属性指定。     另一个是导航控制器根视图控制器,它一般是UIViewController的实例,通过UINavigationController的构造方法     initWithRootViewController:指定,用于提供和呈现导航控制器的一级视图,即我们看到的第一个界面。     */}

- (void)viewDidLoad{    [super viewDidLoad];    self.view.backgroundColor = [UIColor grayColor];        mutArr = [[NSMutableArray alloc] initWithCapacity:20];    for (int i = 0 ; i < 10 ; i ++)    {        NSString *str = [NSString stringWithFormat:@"苹果_%d",i];        [mutArr addObject:str];    }        // 导航栏标题    self.title = @"苹果";        // 导航栏左边和右边按钮    UIBarButtonItem *navLeftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editClick:)];    UIBarButtonItem *navRightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClick:)];    self.navigationItem.leftBarButtonItem = navLeftItem;    self.navigationItem.rightBarButtonItem = navRightItem;    [navLeftItem release];    [navRightItem release];        [self initTableView];}#pragma mark - 初始化UITableView- (void)initTableView{    m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];  // 568    m_tableView.backgroundColor = [UIColor whiteColor];    m_tableView.delegate = self;    m_tableView.dataSource = self;    [self.view addSubview:m_tableView];    [m_tableView release];}#pragma mark - UITableViewDelegate- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [mutArr count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *indentifier = @"myCell";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];        if (cell == nil)    {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier] autorelease];    }        NSString *str = [mutArr objectAtIndex:[indexPath row]];    cell.textLabel.text = str;    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        return cell;}- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    int iRow  = [indexPath row];    NSString *str = [mutArr objectAtIndex:iRow];        DetailViewController *detailViewCtl = [[DetailViewController alloc] init];    detailViewCtl.m_Title = str;    [self.navigationController pushViewController:detailViewCtl animated:YES];    [detailViewCtl release];}- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    if (!tableView.editing)        return UITableViewCellEditingStyleNone;    else {        return UITableViewCellEditingStyleDelete;    }}- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    int iRow = [indexPath row];    [mutArr removeObjectAtIndex:iRow];    [tableView reloadData];}- (void)editClick:(id)sender{    m_tableView.editing = !m_tableView.editing;}- (void)doneClick:(id)sender{    m_tableView.editing = NO;}

//// DetailViewController.h//#import <UIKit/UIKit.h>@interface DetailViewController : UIViewController <UIWebViewDelegate>@property (copy, nonatomic) NSString *m_Title;@end

//// DetailViewController.m//#import "DetailViewController.h"@interface DetailViewController ()@end@implementation DetailViewController- (void)dealloc{    [self.m_Title release];    [super dealloc];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self)    {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    self.title = self.m_Title;        [self intiUI];}- (void)intiUI{    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];  // 568    webView.backgroundColor = [UIColor clearColor];    webView.delegate = self;    [self.view addSubview:webView];    [webView release];        NSString *str = @"http://www.baidu.com";    NSURL *url = [NSURL URLWithString:str];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    [webView loadRequest:request];}- (void)webViewDidFinishLoad:(UIWebView *)webView{    NSLog(@"finish");}- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{    NSLog(@"%@",[error description]);}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}@end

至此,树形结构导航介绍已经完毕,程序运行效果图如下:

ios 6效果图:

      


ios 7效果图:

      

0 0
原创粉丝点击