码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发指南 第7章 视图控制器与导航模式 学习

时间:2015-09-18 20:27:51      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

1 概述

   分类:平铺导航模式 标签导航模式 树形导航模式

2 模态视图 必须要一个单独的模态视图控制器

  呈现 代码方法:presentViewController:animated:completion:

         故事板segue方式

  关闭 dismissViewControllerAnimated:completion:

  获取navigationBar:拖拽一个

                              Editor-Embed in-Navigation Controller  

                              创建一个navigation controller

                              interface builder

  准备:构建controller之间present modally类型的segue方式 

          为模态视图控制器匹配Class

  关闭模态视图:点save将参数用广播的方式传递出去 postNotificationName: object:userInfo:

                      点cancle只关闭模态视图

- (IBAction)save:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        NSLog(@"点击Save按钮,关闭模态视图");
        
        NSDictionary *dataDict = [NSDictionary dictionaryWithObject:self.txtUsername.text
                                                             forKey:@"username"];
        [[NSNotificationCenter defaultCenter]
                postNotificationName:@"RegisterCompletionNotification"
                object:nil
                userInfo:dataDict];要传递的参数字典类型
        
    }];
    
}

- (IBAction)cancel:(id)sender {

[self dismissViewControllerAnimated:YES completion:^{        
 NSLog(@"点击Cancel按钮,关闭模态视图");

    }];}

 

接受数据:也是以广播方式接受addObserver:selector:name:object:

              获取数据[notification userInfo]

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(registerCompletion:)
                                                 name:@"RegisterCompletionNotification"
                                               object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


-(void)registerCompletion:(NSNotification*)notification {
    获取数据
    NSDictionary *theData = [notification userInfo];
    NSString *username = [theData objectForKey:@"username"];
    
    NSLog(@"username = %@",username);
}

3 基于分屏导航实现平铺导航 

   准备:pageControl scrollView 层次上这两个控件没有包含关系是并列的,不能将pageControl拖拽到scrollView里面了,可以使用键盘方向键调整

           设置控件的属性 定义输出口 为pageControl定义相应屏幕变化事件的方法changePage:   

   添加图片素材:在代码中,可以通过storyboard ID获得故事板中视图控制器对象 

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.scrollView.delegate = self;
    设置内容视图contentSize和frame
    self.scrollView.contentSize  = CGSizeMake(self.view.frame.size.width*3, self.scrollView.frame.size.height);
    self.scrollView.frame = self.view.frame;
    
    UIStoryboard *mainStoryboard =  self.storyboard;
    从故事版中获得控制器对象
    UIViewController* page1ViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"page1"];
    self.page1 = page1ViewController.view;
    self.page1.frame = CGRectMake(0.0f, 0.0f, 320.0f, 420.0f);设置图片位置以便放入contentView中
    
    UIViewController* page2ViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"page2"];
    self.page2 = page2ViewController.view;
    self.page2.frame = CGRectMake(320.0f, 0.0f, 320.0f, 420.0f);
    
    UIViewController* page3ViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"page3"];
    self.page3 = page3ViewController.view;
    self.page3.frame = CGRectMake(2 * 320.0f, 0.0f, 320.0f, 420.0f);
    
    [self.scrollView addSubview:self.page1];
    [self.scrollView addSubview:self.page2];
    [self.scrollView addSubview:self.page3];
}

实现scrollView代理方法:滑动时调用

定位pageContro的当前屏 curentpage属性

- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{   用偏移量来定位
    CGPoint offset = aScrollView.contentOffset;
    self.pageControl.currentPage = offset.x / 320.0f;
}

实现分屏控件事件方法:点分屏控件-导致scrollView偏移 动画方式

- (IBAction)changePage:(id)sender
{    动画
    [UIView animateWithDuration:0.3f animations:^{
        NSInteger whichPage = self.pageControl.currentPage;
        self.scrollView.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
    }];
}

4 标签导航 标签栏占49点空间

   标签导航模式的各个标签分别代表一个功能模块,各功能模块相对独立。

   各viewControllers和tabBarController的Relationship segue关系是view controllers

5 树形结构导航

   采用分层组织信息的方式,构建有从属关系的导航。

   不同级控制器segue的方式:show

   选中segue,设置其属性检查器中identifier属性,将用于在代码中查询segue对象,为下一级视图做something。

   各级数据关系:总数据是NSDict *dictData

                      每级表视图有自己的listData,其数据来源于dictData

   1)根视图控制器代码

      视图跳转时会调用:prepareForSegue:sender:

      segue.destinationViewController属性用于获得要跳转的视图控制器对象

 

- (void)viewDidLoad
{
    [super viewDidLoad];
    设置代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    获取文件内容
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *path = [bundle pathForResource:@"provinces_cities" ofType:@"plist"];
    
    self.dictData = [[NSDictionary  alloc] initWithContentsOfFile:path];
    
    self.listData = [self.dictData allKeys];
    
    self.title = @"省份信息";
}
pragma mark - 实现表视图数据源方法
- (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.dictData objectForKey:selectName];
        citiesViewController.title = selectName;
    }
}

 

2)二级视图控制器代码

- (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];
    NSDictionary *dict = [self.listData objectAtIndex:row];
    
    cell.textLabel.text = [dict objectForKey:@"name"];
    
    return cell;
}

//选择表视图行时候触发
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    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"];
    }
}

 3)三级视图控制器代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.webView.delegate = self;
    NSURL * url = [NSURL URLWithString: self.url];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
    
}

#pragma mark WebView 委托
#pragma mark --
- (void)webViewDidFinishLoad: (UIWebView *) webView {
    NSLog(@"finish");
}

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

 

iOS开发指南 第7章 视图控制器与导航模式 学习

标签:

原文地址:http://www.cnblogs.com/haugezi/p/4820114.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!