Runloop运行循环的作用

 Runloop运行循环的作用是什么?

 Runloop运行循环在现在实际开发中,几乎用不到!但是概念非常重要,需要理解!

 作用:

 1. 保证程序不退出!在实际开发中,程序员不需要考虑运行循环!

 2. 监听输入事件:触摸事件,时钟事件,网络事件完成等等都是由运行循环来负责监听的!

              那下面拿按钮的点击实际举例:

  [btn addTarget:self action:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside]

  监听按钮点击一般会使用上面的方法,那系统是如何知道,点击了某个按钮呢?请看下图(涉及到了响应者链条和Runloop监听和注册

  Runloop运行循环的作用_第1张图片

    

3. 时钟:

  

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(fire) userInfo:nil repeats:YES];

    

    //NSRunLoopCommonModes 需要和用户交互的时,同时被触发,可以选择 NSRunLoopCommonModes

    //NSDefaultRunLoopMode 系统默认的

    [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];


注意:时钟执行的方法 ,不能太耗时,如果耗时严重,会影响用户交互!


你可能感兴趣的:(iOS知识点)