zoukankan      html  css  js  c++  java
  • 获取UIVIewController对象的几种方式、NSBundle的简单使用

    学习内容

    欢迎关注我的iOS学习总结——每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary

    UISlider的使用及自定义

    1. 使用
    • .value	//滑块的当前值,如果没有设置最大最小值那么默认为0~1
      .minimum	//滑块最小值
      .maximum	//滑块最大值
      .minimumValueImage	//左侧图片
      .maximumValueImage	//右侧图片
       setThumbImage:forState:	//设置滑块在不同状态下的图片
      .continuous	//设置为yes,value实时改变,设置为no,停止滑动才改变
      
    1. 因为UISlider没有代理方法,所以我们要监听滑块的值改变的话,可以为其绑定事件
    • [_slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
      
    • //自定义UISlider我们不需要自己写一个UIView,添加一个子类继承于UISlider,重写四个方法即可
      //设置UISlider左侧图片的大小
      - (CGRect)minimumValueImageRectForBounds:(CGRect)bounds;
      //设置UISlider右侧图片的大小
      - (CGRect)maximumValueImageRectForBounds:(CGRect)bounds;
      //设置UISlider中间滑块的大小
      - (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;
      //设置滑动条(滑动轨迹)的大小
      - (CGRect)trackRectForBounds:(CGRect)bounds;
      

    关于UIViewController的一些知识点

    1. 得到UIViewController对象的几种方法

      • 通过StoryBoard ID获得对应的UIVIewController

        • //首先根据Main.storyboard的名称注册一个storyBoard实例
          UIStoryboard * sBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
          //使用sBoard对象,实例化一个LTInstantiateController对象,这里的Identifier为StoryBoard中绑定的UIViewController实例
          LTInstantiateController * ltInstance = [sBoard instantiateViewControllerWithIdentifier:@"LTInstaniateController"];
          [self.navigationController pushViewController:ltInstance animated:YES];
          
        • 当StoryBoard中绑定的视图控制器为initialView时,不需要通过storyboard注册,但是从某个控制器跳转到storyboard生成的控制器时,需要使用以上方法生成实例,直接使用alloc,init方法生成的话,storyboard中的控件无法显示。

        • 如果跳转的目标控制器和当前控制器在同一个StoryBoard中的话,可以直接使用self.storyBoard进行注册,self.storybaord就是当前控制器所属的storyboard

      • 通过xib文件获得UIViewController对象

        • LTInstanciateXibController * xibInstance = [[LTInstanciateXibController alloc]initWithNibName:@"LTInstanciateXibController" bundle:[NSBundle mainBundle]];
          [self.navigationController pushViewController:xibInstance animated:YES];
          
        • 在创建VIewController时候如果勾选了xib那么使用该控制器对象时也要通过xib方式注册

      • 通过StoryBoard中的连线得到对应的UIViewController

        • -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
            if ([segue.destinationViewController isKindOfClass:[CustomViewController class]]) {
              CustomViewController *detailsController = (CustomViewController *)segue.destinationViewController;
            }
          }
          
        • 这种获取实例对象的方式需要设置segue的箭头

    2. 视图控制器之间跳转的方式

      • 通过UINavigationController进行push、pop跳转

        • //跳转(入栈)
          [self.navigationController pushViewController:destinationController animated:YES];
          //返回(出栈)
          [self.navigationController popViewControllerAnimated:YES];
          
        • 这种跳转方式类似于入栈、出栈、当前显示的控制器为栈顶控制器

      • 通过UIVIewController自身的present、dismiss进行跳转(这种是模态视图跳转)

        • //跳转到视图
          [self presentViewController:destinationController animated:YES completion:nil]
          //弹出
          [self dismissViewControllerAnimated:YES completion:nil];
          
        • 控制模态视图的弹出风格(modalPresentationStyle)

          //现在使用模态弹出的方式默认是非全屏的,可以通过style设置弹出为全屏,还有其他几种类型,可以对比一下
          destinationVC.modalPresentationStyle = UIModalPresentationFullScreen;
          

    NSBundle的知识点

    1. NSBundle类返回的对象是一个目录,其中包含了程序会使用到的资源,这些资源包含了如图像、声音、编译好的代码、以及.nib文件,当前应用程序所在的目录就是mainBundle

      • //获取主目录下的名称叫mario的类型为png的资源路径
        NSString* path = [[NSBundle mainBundle]pathForResource:@"mario" ofType:@"png"];
        //通过路径获取到图片对象
        UIImage* img = [UIImage imageWithContentsOfFile:path];
        
      • 平时我们在开发中直接使用[UIImage imageNamed:@"mario.png"]是因为编译器自动帮我们把mario.png路径由相对路径转换为绝对路径了,但是如果我们需要加载自定义类型的文件,比如mario.txt那么是无法正常加载的

        • NSString* path = [[NSBundle mainBundle]pathForResource:@"mario" ofType:@"txt"];
          NSFileHandle *fileHandle=[NSFileHandlefile HandleForReadingAtPath:path];
          
    2. bundle文件是静态的,被bundle包含的资源文件作为一个资源包是不参加编译的,bundle包中不能包含可执行的文件,它仅仅是作为资源被解析成特定的二进制数据

    3. 除了使用应用程序的mainBundle,我们也可以自定义Bundle,流程如下

      • newFile->setting Bundle,自定义Bundle名称,里面默认的文件和plist不需要的可以删除

      • 将资源文件拖入自定义Bundle目录下

      • 使用

        • //获取主目录下定义的bundle目录
          NSString * testBundlePath = [[NSBundle mainBundle]pathForResource:@"LTTestBundle" ofType:@"bundle"];
          //从bundle目录中获取图片路径
          NSString* imgPath = [testBundlePath stringByAppendingPathComponent:@"animalFriend.jpg"];
          //根据图片路径取出图片文件
          UIImage * img =  [UIImage imageWithContentsOfFile:imgPath];
          self.testImgView.image = img;
          
  • 相关阅读:
    npm安装elasticsearch-reindex
    Linux14_文本编辑器与终端配置
    Linux13_群组的管理和文件权限的管理
    Linux12_用户和权限
    Linux11_文件及目录以及其相关的组织命令
    Linux10_常用命令和操作
    Linux9_安装Linux系统
    基础概念——什么是POSIX
    C++Review21_宏和常量
    C++Review20_C++函数高级特性(重载、内联、const和virtual)
  • 原文地址:https://www.cnblogs.com/chenprice/p/12905758.html
Copyright © 2011-2022 走看看