zoukankan      html  css  js  c++  java
  • CS193p Lecture 6

    抽象类(Abstract):指的是这个类不能被实例化,只能被继承;

    OC中没有关键词来标明某个类是抽象类,只能在注释中标注一下;

    抽象类中的抽象方法,必须是public的,使方法称为public的方法是,将其声明放置到 .h 文件的interface中;

    Multiple MVCs in an Application

    如何添加多个MVC呢?

    1. 在 object library 中找到 UIViewController,拖拽到 storyboard;

    2. New - File,创建 UIViewController 的子类;

    3. 在 identity inspector 中将storyboard中的 UIViewController 关联到新建的 UIViewController 的子类;

    4. 添加完MVC后,你就可以在 view 中加些button、label,还有outlets、actions之类的;

    如何将多个MVC展现给用户呢?

    (下面这个解释有点抽象,结合后面的例子来理解吧)

    you use a controller whose view is other MVCs.

    有一个控制器,这个控制器的视图是其他MVC。

    UINavigationController

    组成:

    左上角:返回按钮

    上部:标题(title)

    右上角的按钮:an NSArray of UIBarButtonItems

    底部的按钮:an NSArray of UIBarButtonItems

    rootViewController  

    设置根控制器

    每次向 UINavigationController 中压入新MVC时,都是从storyboard中新建一个,在堆中新实例化一个;返回后,它就消失被释放;

    所以这些MVC需要知道如何变为活动状态,准备出现在屏幕上,做要做但事情,完成之后,保存工作进度,然后离开。

    如果某些数据需要继续使用,就需要对它进行保存。可以通过向将你压进来的那个MVC发送消息来实现,需要借助不可视结构化通信(blind structured communication)来实现。

    压入操作(push)

    segue

    A segue is just when you're going to move or segue, from one MVC to another. 

    这里用到的称为:push segue

    scene:表示一个控制器和一个对应视图的组合;

    创建一个segue的方法:

    从segue开始的地方按住control键,拖动至想要segue到的视图控制器上;

    弹出操作(pop)

    方法一:点击左上角返回按钮;

    方法二:

    - (void)popViewControllerAnimated:(BOOL)animated

    [self.navigationController popViewControllerAnimated:YES];

    如果你是一个视图控制器(view controller),且嵌套在一个导航控制器(navigation controller)中,那你就拥有一个属性:navigationController,指向当前所在的导航控制器;

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if (segue.identifier = "DoSomething") {
            if ([segue.destinationViewController isKindOfClass:[DoSomethingVC class]]) {
                DoSomethingVC *doVC = (DoSomethingVC *)segue.destinationViewController;
                doVC.needInfo = ...;
            }
        }
    }

    两个重要属性:

    1. segue.identifier:因为一个视图可能可以segue到不同的视图,所以要通过identifier来区分;

    2. segue.destinationViewController:为了确保segue过去的视图是我们想要的视图类型;

    doVC.needInfo就是segue到目的视图之前需要做的准备工作;

    注意:

    当 prepareForSegue: sender: 被调用时,目标MVC的输出口(outlet)并没有设置好,也就是说,它是位于awakeFromNib和viewDidLoad之间被调用的;

    Demo

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if ([segue.identifier isEqualToString:@"Statistic Text"]) {
            if ([segue.destinationViewController isKindOfClass:[TextStatisticsViewController class]]) {
                TextStatisticsViewController *tsVC = (TextStatisticsViewController *)segue.destinationViewController;
                tsVC.textToAnalyse = self.body.textStorage;
            }
        }
    }
    - (void)setTextToAnalyse:(NSAttributedString *)textToAnalyse{
        _textToAnalyse = textToAnalyse;
        [self updateUI];
    }
    
    - (void)viewWillAppear:(BOOL)animated{
        [super viewWillAppear:animated];
        [self updateUI];
    }
    
    - (void)updateUI{
        self.colorLabel.text = [NSString stringWithFormat:@"%lu colored characters",
                                [[self characterWithAttribute:NSForegroundColorAttributeName] length]];
        self.outlineLabel.text = [NSString stringWithFormat:@"%lu outlined characters",
                                [[self characterWithAttribute:NSStrokeWidthAttributeName] length]];
    }
    
    - (NSAttributedString *)characterWithAttribute:(NSString *)attributeName{
        NSMutableAttributedString *character = [[NSMutableAttributedString alloc] init];
        
        int index = 0;
        
        while (index < [self.textToAnalyse length]) {
            NSRange range;
            id value = [self.textToAnalyse attribute:attributeName atIndex:index effectiveRange:&range];
            if (value) {
                [character appendAttributedString:[self.textToAnalyse attributedSubstringFromRange:range]];
                index = (int)(range.location + range.length);
            } else {
                index ++;
            }
        }
        
        return character;
    }

    分析下这个Demo,虽然比较简单,但是包含对设计模式是很通用的,熟悉之:

      

    UITabBarController

     

  • 相关阅读:
    leetcode33. Search in Rotated Sorted Array
    pycharm 设置sublime text3 monokai主题
    django class Meta
    leetcode30, Substring With Concatenation Of All Words
    Sublime text3修改tab键为缩进为四个空格,
    sublime text3 python打开图像的问题
    安装上imesupport输入法依然不跟随的解决办法,
    sublime text3 的插件冲突弃用问题,
    sublime text3 BracketHighlighter括号匹配的设置
    windows 下wget的使用
  • 原文地址:https://www.cnblogs.com/mobilefeng/p/4394796.html
Copyright © 2011-2022 走看看