ios动态创建控件及添加事件

ops/2024/10/19 17:31:54/

效果如下,就是在一个空白页面动态添加控件,给按钮添加事件,图片名字和标题放入plist文件,plist是个Array,每一项是Dictionary。Dictionary里面方icon和name两个String的key。图片都放入Assets.xcassets。如果需要使用imageWithContentsOfFile方法(无缓存)加载图片,那么就需要新建Supporting Files个Group,将文件夹托进去,使用imageNamed方法(有缓存),那么只需将图片拖入Assets.xcassets文件夹即可。

给出代码:

//
//  AddViewController.m
//  study2024
//
//  Created by zhifei  zhu on 2024/8/31.
//#import "AddViewController.h"@interface AddViewController ()
@property (nonatomic,strong) NSArray *iconArray;
@end@implementation AddViewController
- (NSArray *)iconArray{if(_iconArray==nil){NSString *path=[[NSBundle mainBundle]pathForResource:@"icons.plist" ofType:nil];_iconArray=[NSArray arrayWithContentsOfFile:path];}return _iconArray;
}
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view from its nib.NSUInteger count=self.iconArray.count;for(int a=0;a<count;a++){//添加外边框UIView *uiView=[UIView new];uiView.backgroundColor=[UIColor blueColor];CGFloat x=50+(a%3)*(75+10);CGFloat y=50+(a/3)*(100+10);uiView.frame=CGRectMake(x, y, 75, 100);//x,y,w,h//外边框内部添加图片UIImageView *uiImageView=[UIImageView new];uiImageView.backgroundColor=[UIColor greenColor];CGFloat iconW=45;CGFloat iconH=45;CGFloat x1=(uiView.frame.size.width-iconW)*0.5;//相对坐标CGFloat y1=0;//相对坐标uiImageView.frame=CGRectMake(x1, y1, iconW, iconH);//x,y,w,hNSDictionary *dictionary=self.iconArray[a];
//        NSString *imgPath = [NSString stringWithFormat:@"%@/%@.jpg", [[NSBundle mainBundle] resourcePath], dictionary[@"icon"]];NSString *imgPath=[[NSBundle mainBundle]pathForResource:dictionary[@"icon"] ofType:@"jpg"];//照片拖入Assets.xcassets文件夹会找不到资源,注意需要项目下新建group命名为Supporting Files,再项目外新建文件夹比如icons,然后将图片放入icons,再将icons文件夹拖入Supporting Files才能找到,否则返回nilUIImage *uiImage=[UIImage imageWithContentsOfFile:imgPath];//imageWithContentsOfFile不会缓存,每次都重新加载图片
//        UIImage *uiImage=[UIImage imageNamed:dictionary[@"icon"]];//imageNamed会缓存,照片拖入Assets.xcassets文件夹即可,图片非常多,会占用很多内存uiImageView.image=uiImage;[uiView addSubview:uiImageView];//外边框内部标题UILabel *uiLabel=[UILabel new];uiLabel.backgroundColor=[UIColor yellowColor];CGFloat labelW=uiView.frame.size.width;CGFloat labelH=20;CGFloat x2=0;//相对坐标CGFloat y2=uiImageView.frame.size.height+5;//相对坐标uiLabel.frame=CGRectMake(x2, y2, labelW, labelH);//x,y,w,huiLabel.text=dictionary[@"name"];[uiLabel setTextAlignment:NSTextAlignmentCenter];[uiView addSubview:uiLabel];//外边框内部添加按钮UIButton *uiButton=[UIButton new];uiButton.backgroundColor=[UIColor redColor];CGFloat buttonW=55;CGFloat buttonH=20;CGFloat x3=(75-55)*0.5;//相对坐标CGFloat y3=uiImageView.frame.size.height+uiLabel.frame.size.height+5+5;//相对坐标uiButton.frame=CGRectMake(x3, y3, buttonW, buttonH);//x,y,w,h[uiButton setTitle:@"下载" forState:UIControlStateNormal];[uiButton addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];uiButton.tag=a;[uiView addSubview:uiButton];[self.view addSubview:uiView];}}
-(void)onclick:(UIButton *)uiButton{NSLog(@"%d点击下载",uiButton.tag);
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/@end


http://www.ppmy.cn/ops/104547.html

相关文章

Android使用addr2line分析Native Crash

NDK提供的工具将函数地址解析为具体的函数名和行数才能进一步分析问题。 常用的地址转换工具有addr2line、ndk-stack等&#xff0c;个人比较喜欢addr2line&#xff0c;所以接下来介绍下该工具的基本使用方式 日常使用过程中&#xff0c;只需要关注-C -f -e三个参数即可 // -…

Docker下使用llama.cpp部署带Function calling和Json Mode功能的Mistral 7B模型

Docker下使用llama.cpp部署带Function calling和Json Mode功能的Mistral 7B模型 说明&#xff1a; 首次发表日期&#xff1a;2024-08-27参考&#xff1a; https://www.markhneedham.com/blog/2024/06/23/mistral-7b-function-calling-llama-cpp/https://github.com/abetlen/l…

慷智serdes调试记录(AIM951-958)

项目使用的是AIM951~958 951和958测试图谱输出 下面是两种1920*1080不同屏幕timing的951图谱输出&#xff08;一般我们提供屏幕timing&#xff0c;然后找慷智FAE要下寄存器配置&#xff0c;他有一个xcel文档&#xff0c;可以自己设置屏幕timing然后得到寄存器设置值&#xff…

uniapp、微信小程序车牌的录入的解决方案

结合uv-ui进行编写&#xff0c;键盘使用uv-ui的组件&#xff0c;其他由我们自己编写。 <template><div class"addCarContent"><div class"boxContent"><div class"carCodeInput" click"getIndex"><div:cl…

【MySQL数据库管理问答题】第14章 使用 MySQL InnoDB 集群实现高可用性

目录 1. 结合“体系结构”&#xff0c;请说明你对 InnoDB 集群的整体认知。 2. 请对组复制的原理和功能做一个完整的描述&#xff0c;并说明组复制有哪些先决条件和限制。 3. MySQL Shell (mysqlsh)和 MySQL Router (mysqlrouter) 各自提供了什么样的集群管理功能&#xff1…

3.服务注册_服务发现

文章目录 1.服务注册_服务发现1.1服务注册概念及图解介绍2.2 CAP理论2.3 常见的注册中心(了解)2.4 Eureka组件介绍2.4.1.搭建注册中心2.4.2服务注册2.4.3服务发现 大家好&#xff0c;我是晓星航。今天为大家带来的是 服务注册_服务发现 相关的讲解&#xff01;&#x1f600; 1…

javaSSMmysql宠物领养系统的设计与实现26292-计算机毕业设计项目选题推荐(附源码)

摘 要 如今&#xff0c;随着人们生活水平不断提高&#xff0c;人们的生活在物质满足的基础上&#xff0c;更多的人将生活的重点放在追求精神享受的过程中。于此同时&#xff0c;Internet铺天盖地的普及&#xff0c;使得这样的人纷纷通过Internet的方式去寻找精神的满足。然而领…

美股DT有没有程序化软件或者指标选股工具

炒股自动化&#xff1a;申请官方API接口&#xff0c;散户也可以 python炒股自动化&#xff08;0&#xff09;&#xff0c;申请券商API接口 python炒股自动化&#xff08;1&#xff09;&#xff0c;量化交易接口区别 Python炒股自动化&#xff08;2&#xff09;&#xff1a;获取…