ios通过xib创建控件

news2025/2/12 9:19:56

之前写过ios动态创建控件及添加事件,纯手工代码写控件,虽然比较灵活,但是就是代码量比较多。这次我们通过xib来创建app下载列表项 AppView.xib。一个imageview,一个label,一个button构成

1.创建AppView.xib

2.再创建xib对应的mode,AppView.h 继承至UIView

实现效果如下:

3.xib页面设计好了之后,将控件拖入AppView.h

//
//  AppView.h
//  iosstudy2024
//
//  Created by figo on 2025/2/10.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface AppView : UIView
@property (weak, nonatomic) IBOutlet UIImageView *iconImg;
@property (weak, nonatomic) IBOutlet UILabel *appName;
@property (weak, nonatomic) IBOutlet UIButton *btnDownload;

@end

NS_ASSUME_NONNULL_END

4.列表配置文件icons.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
	<dict>
		<key>name</key>
		<string>deepseek</string>
		<key>icon</key>
		<string>diamond</string>
	</dict>
	<dict>
		<key>name</key>
		<string>文心一言</string>
		<key>icon</key>
		<string>star</string>
	</dict>
	<dict>
		<key>name</key>
		<string>豆包</string>
		<key>icon</key>
		<string>grape</string>
	</dict>
	<dict>
		<key>name</key>
		<string>deepseek</string>
		<key>icon</key>
		<string>watermenon</string>
	</dict>
	<dict>
		<key>name</key>
		<string>deepseek</string>
		<key>icon</key>
		<string>diamond</string>
	</dict>
	<dict>
		<key>name</key>
		<string>deepseek</string>
		<key>icon</key>
		<string>diamond</string>
	</dict>
	<dict>
		<key>name</key>
		<string>deepseek</string>
		<key>icon</key>
		<string>diamond</string>
	</dict>
	<dict>
		<key>name</key>
		<string>deepseek</string>
		<key>icon</key>
		<string>diamond</string>
	</dict>
	<dict>
		<key>name</key>
		<string>deepseek</string>
		<key>icon</key>
		<string>diamond</string>
	</dict>
</array>
</plist>

5.添加控制器AppDownloadViewController.m

//
//  AppDownloadViewController.m
//  iosstudy2024
//
//  Created by figo on 2025/2/10.
//

#import "AppDownloadViewController.h"
#import "AppView.h"
@interface AppDownloadViewController ()
@property (nonatomic,strong) NSArray *iconArray;

@end

@implementation AppDownloadViewController
- (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.
    //[self initApp];
    [self initAppUseXib];
    
    
}

-(void) initApp{
    // Do any additional setup after loading the view from its nib.
       NSUInteger count=self.iconArray.count;
       NSBundle *boundle=[NSBundle mainBundle];
       for(int a=0;a<count;a++){
           //添加外边框
           UIView *uiView=[UIView new];//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,h
           NSDictionary *dictionary=self.iconArray[a];
   //        NSString *imgPath = [NSString stringWithFormat:@"%@/%@.jpg", [[NSBundle mainBundle] resourcePath], dictionary[@"icon"]];
           NSString *imgPath=[[NSBundle mainBundle]pathForResource:dictionary[@"icon"] ofType:@".jpeg"];
           //照片拖入Assets.xcassets文件夹会找不到资源,注意需要项目下新建group命名为Supporting Files,再项目外新建文件夹比如icons,然后将图片放入icons,再将icons文件夹拖入Supporting Files才能找到,否则返回nil
           UIImage *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,h
           uiLabel.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) initAppUseXib{
    // Do any additional setup after loading the view from its nib.
       NSUInteger count=self.iconArray.count;
       NSBundle *boundle=[NSBundle mainBundle];
       for(int a=0;a<count;a++){
           //添加外边框
           //通过xib文件获取的方式
           AppView *uiView=[[boundle loadNibNamed:@"AppView" owner:nil options:nil]lastObject];
           uiView.backgroundColor=[UIColor purpleColor];
           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
           
           //外边框内部添加图片
           uiView.iconImg.backgroundColor=[UIColor greenColor];
           NSDictionary *dictionary=self.iconArray[a];
           NSString *imgPath=[[NSBundle mainBundle]pathForResource:dictionary[@"icon"] ofType:@".png"];
           //照片拖入Assets.xcassets文件夹会找不到资源,注意需要项目下新建group命名为Supporting Files,再项目外新建文件夹比如icons,然后将图片放入icons,再将icons文件夹拖入Supporting Files才能找到,否则返回nil
           UIImage *uiImage=[UIImage imageWithContentsOfFile:imgPath];//imageWithContentsOfFile不会缓存,每次都重新加载图片
//           UIImage *uiImage=[UIImage imageNamed:dictionary[@"icon"]];//imageNamed会缓存,照片拖入Assets.xcassets文件夹即可,图片非常多,会占用很多内存
           uiView.iconImg.image=uiImage;
           
           //外边框内部标题
           uiView.appName.backgroundColor=[UIColor yellowColor];
           uiView.appName.text=dictionary[@"name"];
           [uiView.btnDownload addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchUpInside];
           uiView.btnDownload.tag=a;
           [self.view addSubview:uiView];
    
       }
}

-(void)onclick:(UIButton *)uiButton{
    NSLog(@"%d点击下载",uiButton.tag);
}
@end

6.SceneDelegate.m修改当前controller为AppDownloadViewController

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
 
    AppDownloadViewController * viewController = [[AppDownloadViewController alloc]init];
    self.window.rootViewController=viewController;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2296758.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【树莓派Pico设备驱动】-WS2812B全彩LED驱动(基于SPI)

WS2812B全彩LED驱动(基于SPI) 文章目录 WS2812B全彩LED驱动(基于SPI)1、WS2812介绍2、WS2812配置4、驱动实现1、WS2812介绍 WS2812/WS2812B LED 使用 24 位来表示绿色、红色和蓝色值。 WS2812采用单线通信的设计,通信协议为非归零编码,每个LED需要24个bit的数据,数据依…

AIGC-微头条爆款文案创作智能体完整指令(DeepSeek,豆包,千问,Kimi,GPT)

Unity3D特效百例案例项目实战源码Android-Unity实战问题汇总游戏脚本-辅助自动化Android控件全解手册再战Android系列Scratch编程案例软考全系列Unity3D学习专栏蓝桥系列AIGC(GPT、DeepSeek、豆包、千问、Kimi)👉关于作者 专注于Android/Unity和各种游戏开发技巧,以及各种资…

2025届优秀创新大数据毕业设计

吊打导师的大数据毕业设计项目 985华南理工大学学长 大厂全栈&#xff0c;大数据开发工程师 专注定制化开发

解决 ComfyUI-Impact-Pack 中缺少 UltralyticsDetectorProvider 节点的问题

解决 ComfyUI-Impact-Pack 中缺少 UltralyticsDetectorProvider 节点的问题 1. 安装ComfyUI-Impact-Pack 首先确保ComfyUI-Impact-Pack 已经下载 地址: https://github.com/ltdrdata/ComfyUI-Impact-Pack 2. 安装ComfyUI-Impact-Subpack 由于新版本的Impact Pack 不再提供这…

SpringBoot中的Javaconfig

为什么要使用Javaconfig&#xff1f; 如果要声明的bean对象&#xff0c;来自于第三方jar包&#xff08;不是自定义的&#xff09;&#xff0c;无法使用Component 及衍生注解来声明bean&#xff0c;因为第三方的jar一般不可写&#xff0c;需要使用注解Configuration和Bean注解来…

【前端】几种常见的跨域解决方案代理的概念

几种常见的跨域解决方案&代理的概念 一、常见的跨域解决方案1. 服务端配置CORS&#xff08;Cross-Origin Resource Sharing&#xff09;&#xff1a;2. Nginx代理3. Vue CLI配置代理&#xff1a;4 .uni-app在manifest.json中配置代理来解决&#xff1a;5. 使用WebSocket通讯…

Windows11+PyCharm利用MMSegmentation训练自己的数据集保姆级教程

系统版本&#xff1a;Windows 11 依赖环境&#xff1a;Anaconda3 运行软件&#xff1a;PyCharm 一.环境配置 通过Anaconda Prompt(anaconda)打开终端创建一个虚拟环境 conda create --name mmseg python3.93.激活虚拟环境 conda activate mmseg 4.安装pytorch和cuda tor…

基于java手机销售网站设计和实现(LW+源码+讲解)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…

如何评估云原生GenAI应用开发中的安全风险(下)

以上就是如何评估云原生GenAI应用开发中的安全风险系列中的上篇内容&#xff0c;在本篇中我们介绍了在云原生AI应用开发中不同层级的风险&#xff0c;并了解了如何定义AI系统的风险。在本系列下篇中我们会继续探索我们为我们的云原生AI应用评估风险的背景和意义&#xff0c;并且…

使用WebUI访问本地Deepseek(Ollama集成Open WebUI)

在《deepseek本地部署和使用&#xff08;Linux虚拟机&#xff09;》中&#xff0c;我们使用Ollama部署了Deepseek-r1&#xff0c;但是只能通过命令行方式交互&#xff0c;默认Ollama启动后&#xff0c;会启动一个监听到127.0.0.1&#xff0c;用以接收POST 请求&#xff0c;服务…

Word成功接入DeepSeek详细步骤

原理 原理是利用Word的VBA宏&#xff0c;写代码接入API。无需下载额外插件。 步骤一、注册硅基流动 硅基流动统一登录 注册这个是为了有一个api调用的api_key&#xff0c;有一些免费的额度可以使用。大概就是这个公司提供token&#xff0c;我们使用这个公司的模型调用deepsee…

房价预测/矿藏勘探/自然灾害预测……AI助力地球科学革新,浙大/清华/Google Research等已发表重要成果

地球科学作为一个高度跨学科的领域&#xff0c;正在经历一场由 AI 引领的重大变革。回顾 2024 年&#xff0c;研究人员在智慧城市建设、房价预测、海洋生态建模、地面沉降预测、洪水预测、山体滑坡预测、矿物预测等方面取得了一系列突破性成果。这些研究不仅展现了 AI 在处理复…

Linux网络编程--Udp套接字+实战 (万字详解,超详细!!)

目录 套接字协议&#xff1a; 协议(protocol)&#xff1a; 创建套接字(Create Socket): 绑定服务器地址 开始通信 Udp服务器设计--V1 Udp服务器设计--V2 引入进程池 待更新 套接字协议&#xff1a; 协议(protocol)&#xff1a; 如果2个距离很远的人想要进行交流&#xff…

玩转工厂模式

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 什么是工厂模式?工厂方法模式适合应用场景实现方式工厂方法模式优缺点什么是工厂模式? 工厂方法模式是一种创建型设计模式,其在父类中提供一个创建对象的方法,允许子类决定实例化对象的类型。…

开箱即用:一个易用的开源表单工具!

随着互联网的普及&#xff0c;表单应用场景越来越广泛&#xff0c;从网站注册、调查问卷到考试测评&#xff0c;无处不在。传统的表单制作方式需要一定的代码基础&#xff0c;对于不懂编程的小伙伴来说&#xff0c;无疑是一道门槛。 今天&#xff0c;给大家分享一款开源的表单…

基于微信小程序的博物馆预约系统的设计与实现

hello hello~ &#xff0c;这里是 code袁~&#x1f496;&#x1f496; &#xff0c;欢迎大家点赞&#x1f973;&#x1f973;关注&#x1f4a5;&#x1f4a5;收藏&#x1f339;&#x1f339;&#x1f339; &#x1f981;作者简介&#xff1a;一名喜欢分享和记录学习的在校大学生…

fullcalendar全局日历深度定制(自适应、月、周视图)

首先看效果&#xff1a; 把日程通过蓝色标记点标记出来&#xff0c;可展开收起日历。展开为月视图&#xff0c;收起为月-周视图&#xff0c;且把日程展示在日历下面。 涉及功能点有&#xff1a; 日历头部自定义头部星期格式修改主体样式修改日程自定义展开收起展示不同视图(月…

关于浏览器缓存的思考

问题情境 开发中要实现一个非原生pdf预览功能&#xff0c;pdf链接放在一个固定的后台地址&#xff0c;当重新上传pdf后&#xff0c;预览pdf仍然是上一次的pdf内容&#xff0c;没有更新为最新的内容。 查看接口返回状态码为 200 OK(from disk cache)&#xff0c; 表示此次pdf返回…

16vue3实战-----动态路由

16vue3实战-----动态路由 1.思路2.实现2.1创建所有的vue组件2.2创建所有的路由对象文件(与上述中的vue文件一一对应)2.3动态加载所有的路由对象文件2.4根据菜单动态映射正确的路由2.5解决main页面刷新的问题2.6解决main的第一个页面匹配显示的问题2.7根据path匹配menu 1.思路 …

Linux常见命令——系统定时任务

文章目录 crontab 服务管理crontab -e :编辑crontab 定时任务crontab -l 查看crontab 任务crontab -r 删除当前用户所有的crontab 任务 crontab 服务管理 systemctl status crond该系统进程是开机自启动&#xff0c;并且被打开了&#xff0c;可以使用。 crontab -e :编辑cr…