iOS编程第四版笔记

    • First App
      • create project

        image
      • run project

        image
      • MVC

        image
        • View = Button,Label
        • C=ViewController
        • M=Data
      • 设计Quiz

        image
      • 完善工程

        • 创建界面

          image
        • 创建关联

          • 关联类型

            • 插座变量
              • 指向对象的一个指针
              • 比如指向文本框,在动作中可以修改文本框内容
            • 动作
              • 用户点击的一个响应处理方法
          • 使用方法

            image
            • ctrl+选中界面对象

            • 拖动到m中不同位置,会自动生成插座变量和动作

            • 关联不同的action

              image
        • 说明

          • .h文件 定义

          • .m文件 实现

          • 初始化函数
            -(id) initWithCoder:(NSCoder *)aDecoder
            {
            self = [super initWithCoder:aDecoder];
            if(self) {
            self.questions=@[@"What is 7+7?",@"What is 8+7?"];
            self.answers = @[@"14",@"15"];
            self.currentIndex = 0;
            }

            return self;
            

            }

            • (id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
              {
              self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
              if(self) {
              self.questions=@[@"What is 7+7?",@"What is 8+7?"];
              self.answers = @[@"14",@"15"];
              self.currentIndex = 0;
              }

              return self;
              }

    • Objective-c
      • 对象
        • 对象跟结构类似
        • 对象有方法
      • 使用对象
        • 定义
          • Party *p;
        • 初始化
          • Party *p = [ Party alloc];
          • [p init];
        • 调用消息
          • [p somePerson]
          • [p add: Liming type: M]
        • 释放对象
          • p=nil
      • 命令行
        • 命令行入口
          • int main(int argc,const char * argv[])
      • 子类
        • 声明类

          • @interface RItem :NSObject 开始
          • @end 结束
          • 开始和结束中定义变量和方法
          • 这些方法都是公开的
        • 实现类

          • @implement RItem
        • 发送消息语法

          • 点语法
            • 对象只能来处理变量
            • item.valueInDollars = 5;
          • 方括号语法
            • [item setValueInDollars:5];
        • 方法

          • 类方法
            • 使用+号声明
            • 跟定义的类有关系
          • 实例方法
            • 使用-号声明
            • 根具体对象有关
        • 初始化方法

          • init

          • 指定初始化方法,一直没明白如何指定,好像是个约定

            • 需要调用super来初始化父类
          • instancetype

            • 表示类的指针
            • 初始化都返回该类型
            • 不返回RItem *的问题在于子类的继承重写问题
          • id

            • instancetype之前的
          • self

            • 子类
          • super

          • 初始化链

            image
        • import

          • import

          • @import
    • ARC 管理内存
        • 执行某个方法时,需要保存方法中的局部变量

        • 调用方法也会压入栈

          image
        • 大小是固定的

        • 生成新对象在堆中分配内存,大小不固定
        • 指针保存堆里面的对象的地址
      • 对象所有权

        • 方法指向对象的局部变量,该变量拥有所指向的对象
        • 如果对象的实例变量指向其他对象,可以称为拥有该实例所指向的对象
        • 释放所有权
          • 指向另外一个对象
          • 设置变量为nil
          • 释放对象的某个拥有者
          • 从数组中删除对象
      • 强引用弱引用

        • __weak RItem *_container;

        • 循环引用

          image
        • 使用弱引用解决问题

          image
      • 属性

        • @property
        • type
          • nonatomic
          • readwrite
          • strong
          • weak
          • copy
    • 视图和视图层次结构
      • 绘制过程

        • 视图绘制自己
        • 所有视图的图层组合成一幅图像,绘制到屏幕
      • 视图基础

        • 视图都是UIView对象或者是其的子类

        • 视图知道如何绘制自己

        • 视图可以处理事件

        • 视图按层级结构排列,顶层为应用窗口

          image
      • 设置默认ViewController

        • AppDelegate.m文件
        • didFinishLaunchingWithOptions方法
          • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Override point for customization after application launch.
            [self handleFirstView];

            return YES;
            }

        • 设置ViewController
          • (void) handleQuiz {
            self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

            QuizViewController *quizVC = [ [ QuizViewController alloc ] init ];
            self.window.rootViewController = quizVC;
            self.window.backgroundColor = [ UIColor whiteColor];
            [self.window makeKeyAndVisible];
            }

      • 自定义绘图

        • 集成UIView

        • 重写drawRect

          • (void)drawRect:(CGRect)rect
            {
            // [ self drawOneCircle];
            [self drawMulCircle];
            }

          • (void) drawMulCircle
            {
            CGRect bounds = self.bounds;
            CGPoint center;
            center.x = bounds.origin.x + bounds.size.width/2.0f;
            center.y = bounds.origin.y + bounds.size.height/2.0f;

            float maxRadius = (hypot(bounds.size.width,bounds.size.height)/2.0f);

            UIBezierPath *path = [[UIBezierPath alloc] init];

            for(int currentRadius=maxRadius;currentRadius > 0 ; currentRadius -=20) {
            [ path moveToPoint:CGPointMake(center.x+currentRadius,center.y)];
            [path addArcWithCenter:center radius:currentRadius startAngle:0.0f endAngle:M_PI*2.0 clockwise:YES];
            path.lineWidth=5;
            [self.circleColor setStroke];
            [path stroke];
            }

          }

      • 绘制

        • CGRect
        • CGPoint
        • UIBezierPath
          • moveToPoint
          • addArcWithCenter
          • stroke
      • 绘制图像

        • UIImage *logo = [UIImage imageNamed:@"logo.png"];
        • [logo drawInRect:someRect]
    • 运行循环和重绘视图
      • [view setNeedsDisplay]
      • 类扩展
        • 类扩展存放私有变量和方法
        • 类定义存放公开变量和方法
        • 类定义在.h的文件内
          • @interface RHView
          • @end
        • 类扩展在.m的文件内
          • @interfaceRHView()
          • @end
      • 使用UIScrollView
        • scrollview add subview
        • window add scrollview
    • 视图控制器
      • 继承UIViewController

        • 重写loadView方法
        • 通过nib文件传入初始化方法
      • 使用自定义的UIViewController

        image
        • 通过代码设置window的rootViewController变量

          • (void) handleViewController {
            self.window = [[ UIWindow alloc ] initWithFrame:[[UIScreen mainScreen ] bounds]];
            RHyponosisViewController *hvc = [[ RHyponosisViewController alloc] init];
            // self.window.rootViewController = hvc;

            NSBundle *appBundler = [NSBundle mainBundle];
            RReminderViewController *rvc = [[ RReminderViewController alloc ] initWithNibName:@"RReminderViewController" bundle:appBundler];

            self.window.rootViewController = rvc;
            self.window.backgroundColor = [ UIColor whiteColor];
            [self.window makeKeyAndVisible ];

          }

          • 调用 initWithNibName:@"NIbFileName" 初始化创建ViewController
          • 设置window的rootViewController
        • 重写appdelegate.m的setRootViewController方法

          • 设置viewController的view给window
          • 赋值_rootViewController
      • UITabBarController

        • 可以设置多个viewcontroller
        • 底部工具栏切换多个viewcontroller的显示
      • 视图控制器生命周期

        • application:didFinishLaunchingWithOptions
          • 启用应用调用一次
        • initWithNibName:bundle
        • loadView
        • viewDidLoad
        • viewWillAppear
    • 委托和文本输入
      • UIResponder
        • 事件的处理类
        • 几个子类
          • UIView
          • UIViewController
          • UIApplication
        • 焦点
          • becomeFirstResponder //请求焦点
          • resignFirstResponder //放弃焦点
      • 委托
        • textField.delegate = self
          -(void) loadView
          {
          RHypnosisView *backgroundView = [[RHypnosisView alloc] init];

          CGRect textFieldRect = CGRectMake(40,70,240,30);
          UITextField *textField = [[UITextField alloc ] initWithFrame:textFieldRect ];
          textField.borderStyle = UITextBorderStyleRoundedRect;
          textField.placeholder = @"Hypnotize me";
          textField.returnKeyType = UIReturnKeyDone;
          
          textField.delegate = self;
          
          [backgroundView addSubview:textField];
          self.view = backgroundView;
          

          }

        • self 实现UITextFieldDelegate<NSObject>协议
          @interface RHyponosisViewController () <UITextFieldDelegate>
          @end

          @protocol UITextFieldDelegate <NSObject>
          @optional

          • (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
          • (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
          • (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
          • (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
          • (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
          • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
          • (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
          • (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
            @end

          //回车按钮实现​​​

          • (BOOL)textFieldShouldReturn:(UITextField *)textField;
            {
            NSLog(@"%@",@"click return key");
            [self drawHypnoticMessage:textField.text];
            textField.text=@"";
            [textField resignFirstResponder];

            return YES;
            }
            ​​​
            ​​​

        • delegate都是弱引用,避免强引用循环

          • AViewController 包含视图B
          • B的委托指向AViewController
          • 如果委托是强引用类型,那么久会出现强引用循环
      • 添加视图到屏幕某个位置
        • 设置B的frame属性
        • view addSubview:B
      • 调试器
        • 如果需要查看回调堆栈,断点打在init函数处
        • 可以查看生成对象的堆栈
        • 避免多线程引起的位置偏差
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,635评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,628评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,971评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,986评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,006评论 6 394
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,784评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,475评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,364评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,860评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,008评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,152评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,829评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,490评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,035评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,428评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,127评论 2 356

推荐阅读更多精彩内容