iOS编程第四版笔记

    • First App
      • create project

        iOS编程第四版笔记_第1张图片
        image
      • run project

        iOS编程第四版笔记_第2张图片
        image
      • MVC

        iOS编程第四版笔记_第3张图片
        image
        • View = Button,Label
        • C=ViewController
        • M=Data
      • 设计Quiz

        iOS编程第四版笔记_第4张图片
        image
      • 完善工程

        • 创建界面

          iOS编程第四版笔记_第5张图片
          image
        • 创建关联

          • 关联类型

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

            iOS编程第四版笔记_第6张图片
            image
            • ctrl+选中界面对象

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

            • 关联不同的action

              iOS编程第四版笔记_第7张图片
              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

          • 初始化链

            iOS编程第四版笔记_第8张图片
            image
        • import

          • import

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

        • 调用方法也会压入栈

          iOS编程第四版笔记_第9张图片
          image
        • 大小是固定的

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

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

        • __weak RItem *_container;

        • 循环引用

          iOS编程第四版笔记_第10张图片
          image
        • 使用弱引用解决问题

          iOS编程第四版笔记_第11张图片
          image
      • 属性

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

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

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

        • 视图知道如何绘制自己

        • 视图可以处理事件

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

          iOS编程第四版笔记_第12张图片
          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

        iOS编程第四版笔记_第13张图片
        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协议
          @interface RHyponosisViewController ()
          @end

          @protocol UITextFieldDelegate
          @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函数处
        • 可以查看生成对象的堆栈
        • 避免多线程引起的位置偏差

你可能感兴趣的:(iOS编程第四版笔记)