IOS_代码实现树形导航

来源:互联网 发布:手机淘宝社区 编辑:程序博客网 时间:2024/05/19 09:13

实现下图

  1. 开始显示八大菜系
  2. 点以后显示具体的菜
  3. 再点击之后,搜索百度
  4. 选择NO,用一个WebView显示结果
  5. 选择OK,调用系统浏览器搜索.
使用到的文件 cuisine.plist

AppDelegate.m

创建一个window,一个视图,一个导航,视图作为导航的根视图,导航作为window的根视图.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        // 创建一个window    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];        // 创建一个ViewController    ViewController *vc = [[ViewController alloc] init];        // 创建一个导航 ViewController 要使用 pushViewController 跳转页面就必须要创建一个Navigation,然后使 ViewController 入栈    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];        // 设置根视图控制器    self.window.rootViewController = navigationController;        // 这个是便捷方法,去使被使用对象的主窗口显示到屏幕的最前端。你也可以使用hiddenUIView方法隐藏这个窗口(这是文档说法)    [self.window makeKeyAndVisible];    return YES;}


ViewController.m

TableView就不说了.
实现跳转:
#pragma mark cell被选中调用此消息- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    // 创建第二个视图    DishViewController *DishVC = [[DishViewController alloc] init];    // 赋值给dishs做到传值    DishVC.dishs = [self.datas valueForKey:[self.datas.allKeys objectAtIndex:indexPath.row]];    // 给导航栏赋值    DishVC.title = [self.datas.allKeys objectAtIndex:indexPath.row];    //self 必须在navigation栈中,self.navigationController才不会为空    [self.navigationController pushViewController:DishVC animated:YES]; // 跳转....        // 这句代码好像可以直接跳转而不必设置Navigation//    [self presentViewController:DishVC animated:YES completion:nil];        /*使用通知同样可以传值*/}



DishViewController.m


DishViewController.h添加代码:
@property (nonatomic, retain) NSArray *dishs; // 菜@property (nonatomic, retain) UITableView *tableView;@property (nonatomic, retain) id<URLDelegate> delegate; //委托
把数据加载到TableView中,这句代码可以实现在单元格右边显示箭头.
// 显示右边的小箭头    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
添加协议:
@interface DishViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

实现:- (void)tableView:didSelectRowAtIndexPath:
#pragma mark 选中cell触发此消息- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    row = indexPath.row; // 记录选中的行    webViewController = [[WebViewController alloc] init]; //    self.delegate = webViewController; // 设置代理    webViewController.nameOfDish = [[self.dishs objectAtIndex:row] valueForKey:@"name"];    // 询问是否是使用系统自带的浏览器    [[[UIAlertView alloc] initWithTitle:@"选择" message:@"是否使用系统浏览器" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"OK", nil] show];}
实现:- (void)alertView:clickedButtonAtIndex:
#pragma mark 对alertView的响应/*buttonIndex的值会根据按钮的顺序从0增加.0,1,2,3....*/- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{    if (buttonIndex == 0) {        // 设置标题        webViewController.title = webViewController.nameOfDish;        // 跳转页面        [self.navigationController pushViewController:webViewController animated:YES];    }else{        // 代理        if ([self.delegate respondsToSelector:@selector(openBowser)]) {            [self.delegate openBowser];        }    }}

添加协议:

URLDelegate.h:

@protocol URLDelegate <NSObject>#pragma mark 加工访问web的url- (NSString *) urlstring:(NSString*) nameOfDish;#pragma mark 调用系统自带的浏览器- (void) openBowser;@end

WebViewController

WebViewController.实现协议.
WebViewController.h:
@interface WebViewController : UIViewController<URLDelegate>@property (nonatomic, retain) NSString *nameOfDish; // 菜名@property (nonatomic, retain) UIWebView *webView; @end

WebViewController.m:

@implementation WebViewController- (void)viewDidLoad {    [super viewDidLoad];    [self buildWebView];    [self loadWebPageWithString:[self urlstring:self.nameOfDish]];}#pragma mark 创建一个WebView- (void)buildWebView{    // 初始化WebView    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];    // 自动对页面进行缩放以适应屏幕    [self.webView setScalesPageToFit:YES];        [self.view addSubview:self.webView];}#pragma maek 加载一个页面- (void)loadWebPageWithString:(NSString *) urlstring{    // 把string转成url    NSURL *url = [NSURL URLWithString:urlstring];    // 加载页面需要NSURLRequest    NSURLRequest *request = [NSURLRequest requestWithURL:url];        [self.webView loadRequest:request];}#pragma mark 加工访问web的url- (NSString *) urlstring:(NSString*) nameOfDish{    //ios中http请求遇到汉字的时候,需要转化成UTF-8,用到的方法是:    nameOfDish = [nameOfDish stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    // 百度没有提供搜索接口,不过可以使用链接异步得到结果    return [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@&cl=3",nameOfDish];}#pragma mark 调用系统自带的浏览器- (void)openBowser{    NSURL *url = [NSURL URLWithString:[self urlstring:self.nameOfDish]];    // 调用系统..    [[UIApplication sharedApplication] openURL:url];}@end





0 0
原创粉丝点击