Xcode_7 iOS_9 树形结构导航 Objective-C (17)

1、我们可以使用Master-Detail Application模版来新建项目,但是为了初学的简单性,这里我们还是用SingleViewApplication模版来新建项目,新建完之后把原有的ViewController控件删除,拖动一个NavigationController控件,然后我们把原有的ViewController类的父类改为UITableViewController,然后把root view controller视图控件设置为ViewController类,然后将其中的单元格控件设置一下属性,Identifier为CellIdentifier,Accessory为Disclosure Indicator(这里的VIew视图就作为第一级目录)。


2、然后创建二级目录,新建一个cocoa touch class,名字为CitiesViewController,同样也是继承UITableViewController类,然后拖一个视图控制器控件,然后将第一个视图控制器,也就是root view controller拖拽到新建的二级目录的视图控制器上(用鼠标),在Selection Segue中选择Show,这样再在这个连接线上点击设置属性,Identifier为ShowSelectedProvince(用于查询Segue对象)。然后设置视图控制器的类为CitiesViewController,Identifier为CellIdentifier,Accessory为Detail Disclosure。



3、然后创建三级目录,新建一个cocoa类,名字叫DetailViewController,然后再拖一个视图控制器控件,然后同样的将第二级目录的视图控制器拖到这个视图控制器上,也是选择Selection Segue的show,连接线属性设置,Identifier为DetailViewController,然后把这个类设置为这个视图控制器的类,最后拖拽一个webView控件上去,并且座位输出口。


4、新建一个数据文件:provinces_cities.plist,但是这里为了省事,我只设置了一个链接url:




5、接下来是三个类的代码(包括头文件):

//
//  ViewController.h
//  TreeNavigation
//
//  Created by 侯家奇 on 16/8/30.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UITableViewController

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSDictionary *dicctData;
@property (strong, nonatomic) NSArray *listData;

@end

//
//  ViewController.m
//  TreeNavigation
//
//  Created by 侯家奇 on 16/8/30.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import "ViewController.h"
#import "CitiesViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"provinces_cities" ofType:@"plist"];
    self.dicctData = [[NSDictionary alloc]  initWithContentsOfFile:path];
    self.listData = [self.dicctData allKeys];
    self.title = @"省份信息";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSInteger row = [indexPath row];
    cell.textLabel.text = [self.listData objectAtIndex:row];
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowSelectedProvince"]) {
        CitiesViewController *citiesViewController = segue.destinationViewController;
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        NSString *selectName = [self.listData objectAtIndex:selectedIndex];
        citiesViewController.listData = [self.dicctData objectForKey:selectName];
        citiesViewController.title = selectName;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

//
//  CitiesViewController.h
//  TreeNavigation
//
//  Created by 侯家奇 on 16/8/30.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface CitiesViewController : UITableViewController

@property (weak, nonatomic) NSArray *listData;

@end

//
//  CitiesViewController.m
//  TreeNavigation
//
//  Created by 侯家奇 on 16/8/30.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import "CitiesViewController.h"
#import "DetailViewController.h"

@interface CitiesViewController ()

@end

@implementation CitiesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSInteger row = [indexPath row];
    NSDictionary *dict = [self.listData objectAtIndex:row];
    cell.textLabel.text = [dict objectForKey:@"name"];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.listData count];
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"ShowSelectedCity"]) {
        DetailViewController *detailViewController = segue.destinationViewController;
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        NSDictionary *dict = [self.listData objectAtIndex:selectedIndex];
        detailViewController.url = [dict objectForKey:@"url"];
        detailViewController.title = [dict objectForKey:@"name"];
    }
}


@end

//
//  DetailViewController.h
//  TreeNavigation
//
//  Created by 侯家奇 on 16/8/30.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController <UIWebViewDelegate>

@property (weak, nonatomic) NSString *url;
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

//
//  DetailViewController.m
//  TreeNavigation
//
//  Created by 侯家奇 on 16/8/30.
//  Copyright © 2016年 侯家奇. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.webView.delegate = self;
    NSURL *url = [NSURL URLWithString:self.url];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"finish");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"%@", [error description]);
}

@end



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值