• IOS开发学习笔记025-xib和storyboard


    stotyboard : 描述软件界面,大范围,比较适合整个软件的所有界面

    xib文件的使用:描述软件界面,小范围,比较适合描述小界面

    在xcode新建文件窗口可以看到两个文件,storyboard和empty

    可以在xib文件中直接添加各种控件,并设置相关的属性。

    xib文件的使用,加载文件使用NSBundle

     1 // 创建一行,从xib中加载一行
     2 - (UIView *)createRowView
     3 {
     4     // 加载xib文件,并且创建里面的所有对象,并按顺序保存到数组中
     5     NSArray *allViews = [[NSBundle mainBundle] loadNibNamed:@"Empty" owner:nil options:nil];
     6     //NSLog(@"%ld",allViews.count);
     7    
     8     
     9     // 取出红色的view进行
    10     UIView *view = allViews[0];
    11     
    12     // 设置头像
    13     UIButton *icon = (UIButton *)[view viewWithTag:1];
    14     int index = arc4random_uniform(9);
    15     NSString *iconName  = [NSString stringWithFormat:@"01%d.png",index];
    16     [icon setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
    17     
    18     // 设置姓名
    19     UILabel *lab = (UILabel *)[view viewWithTag:3];
    20     int nameIndex = arc4random_uniform(4);
    21     lab.text =_allNames[nameIndex];
    22     
    23     // 监听删除按钮
    24     UIButton *del = (UIButton *) [view viewWithTag:2];
    25     [del addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    26     
    27     return view;
    28 }

    如果想要在界面上实现连线响应事件,需要改变xib的File's ower 的Custom Class 为要连线的类

    然后在loadNibNamed:@"empty" ower:self option:nil

    其中self就是要连线的类或对象

    File's ower

    1、在xib文件中设置File's ower的class属性,目的是在xib文件中能找到owner的方法

    2、建立File's owner 跟控件之间的联系

    3、利用代码加载xib文件,传递owner参数(类型一定要匹配)

    xib的封装实现

    添加一个RowView类将rowView的操作封装起来

    然后添加一个类方法返回初始化后的对象

    然后实现方法如下

     1 + (UIView *)rowViewWithIcon:(NSString *)iconName andName:(NSString *)name
     2 {
     3     // 获得xib文件中得对象
     4     UIView *child = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:self options:nil][0];
     5     // 设置头像
     6     UIButton *icon = (UIButton *)[child viewWithTag:1];
     7     [icon setImage:[UIImage imageNamed:iconName] forState:UIControlStateNormal];
     8   
     9     // 设置标签
    10     UILabel *lab = (UILabel *)[child viewWithTag:2];
    11     lab.text = name;
    12     
    13     return child;
    14 
    15 }

     这样写的话对按钮的响应有两种方式

    第一种是改变xib文件的class为按钮响应方法的类,然后通过连线方式响应

    第二种是通过代码直接绑定事件,要注意

     1 // 通过xib创建一行
     2 - (UIView *)createRowView
     3 {
     4     // 返回view对象
     5     NSString *iconName = [NSString stringWithFormat:@"01%d.png",arc4random_uniform(9)];
     6     UIView *rowView = [RowView rowViewWithIcon:iconName andName:_allNames[arc4random_uniform(5)]];
     7     // 设置头像
     8     UIButton *icon = (UIButton *)[rowView viewWithTag:1];
     9     // 头像按钮监听事件
    10     [icon addTarget:self action:@selector(iconClick:) forControlEvents:UIControlEventTouchUpInside];
    11     
    12     // 设置删除按钮监听事件
    13     UIButton *delBtn = (UIButton *)[rowView viewWithTag:3];
    14     // 添加删除按钮监听事件
    15     [delBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    16 
    17     return rowView;
    18 }

    源代码:

    链接: http://pan.baidu.com/s/1kT3lFfh 密码: 927c

  • 相关阅读:
    开博说两句
    学习总结 (持续更新)
    ip代理 120203
    [vs2005]关于预编绎网站的问题[已预编译此应用程序的错误]
    JAVA类基础
    集合类和泛型
    IO流——字符流
    多线程和包
    多态和内部类
    抽象类与接口
  • 原文地址:https://www.cnblogs.com/songliquan/p/4503736.html
Copyright © 2020-2023  润新知