news 2026/4/30 13:37:47

UI学习:单例传值

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
UI学习:单例传值

文章目录

  • 单例传值
    • 什么是单例
    • 举例讲解

单例传值

什么是单例

单例 = 全局唯一的对象,任何地方都能访问它

举例讲解

VCSecond 有一个 TextField,输入文字后通过通知传给 VCFirst 的 Label 显示

创建单例类, 设置要共享的数据

// DataManager.h@interfaceDataManager:UIViewController// 获取单例的方法-(instancetype)shareManager;// 要共享的数据@property(nonatomic,copy)NSString*shareText;@end

实现单例, 保证每次创建出的DataManager对象都唯一

// DataManager.m// 实现单例+(instancetype)shareManager{staticDataManager*instance=nil;staticdispatch_once_t onceToken;dispatch_once(&onceToken,^{instance=[[superallocWithZone:NULL]init];});returninstance;}+(instancetype)allocWithZone:(struct_NSZone*)zone{return[selfshareManager];}-(id)copyWithZone:(NSZone*)zone{returnself;}-(id)mutableCopy:(NSZone*)zone{returnself;}

定义VCFirst 的showLabel属性 , 显示接收到的值

#import<UIKit/UIKit.h>#import"VCSecond.h"#import"DataManager.h"// 引入单例@interfaceVCFirst:UIViewController@property(nonatomic)UILabel*showLabel;@end

创建label, button,显示传值的内容, 并通过事件调用切换视图控制器

-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor=[UIColor whiteColor];self.title=@"VCFirst";// 创建显示 Labelself.showLabel=[[UILabel alloc]initWithFrame:CGRectMake(50,250,300,50)];self.showLabel.textAlignment=NSTextAlignmentCenter;self.showLabel.backgroundColor=[UIColor lightGrayColor];[self.view addSubview:self.showLabel];// 创建跳转按钮UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithTitle:@"next"style:UIBarButtonItemStylePlain target:selfaction:@selector(pushToSecond)];self.navigationItem.rightBarButtonItem=item;}

每次都从单例中读取数据,因为实现了单例, 所以每次创建的 DataManager 对象都唯一, 因此DataManager 对象可以作为媒介在不同的视图控制器间传递数据

// VCFirst.m// 从单例中读取数据NSString*saveText=[DataManager shareManager].shareText;if(saveText.length>0){self.showLabel.text=[NSString stringWithFormat:@"上次保存: %@",saveText];}else{self.showLabel.text=@"等待接收文字...";}

设置按钮事件, 创建VCSecond视图并跳转

// VCFirst.m-(void)pushToSecond{VCSecond*vc=[[VCSecond alloc]init];[self.navigationController pushViewController:vc animated:YES];}

通过viewWillAppear: (BOOL)animate方法在每次加载到VCFirst的时候都更新数据

// VCFirst.m-(void)viewWillAppear:(BOOL)animated{[superviewWillAppear:animated];// 每次回到这个界面时, 重新读取单例中的最新数据NSString*latesText=[DataManager shareManager].shareText;if(latesText.length>0){self.showLabel.text=[NSString stringWithFormat:@"上次保存: %@",latesText];}}

在VCSsecond中定义并创建 UITextField 类型属性 inputTextField, 用于输入需要传值的内容

// VCSecond.m@interfaceVCSecond()@property(nonatomic,strong)UITextField*inputTextField;@property(nonatomic,strong)UIButton*sendButton;@end-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColor whiteColor];self.title=@"VCSecond";// 创建输入框self.inputTextField=[[UITextField alloc]initWithFrame:CGRectMake(50,250,275,44)];self.inputTextField.borderStyle=UITextBorderStyleRoundedRect;self.inputTextField.placeholder=@"请输入文字";[self.view addSubview:self.inputTextField];// 创建发送按钮UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStylePlain target:selfaction:@selector(pressSave)];self.navigationItem.rightBarButtonItem=item;}

设置按钮事件, 返回 VCFirst 并将 VCSecond 中inputTextField 的text值保存到单例中, 用于VCFirst 中通过单例接受传递的值

// VCSecond.m-(void)pressSave{NSString*inputText=self.inputTextField.text;if(inputText.length==0){inputText=@"空消息";}// 把数据存入单例[DataManager shareManager].shareText=inputText;NSLog(@"已保存到单例: %@",inputText);// 返回[self.navigationController popViewControllerAnimated:YES];}

效果如下

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/30 13:37:21

强力AI填充插件Fillinger:3分钟掌握20倍效率提升的完整指南

强力AI填充插件Fillinger&#xff1a;3分钟掌握20倍效率提升的完整指南 【免费下载链接】illustrator-scripts Adobe Illustrator scripts 项目地址: https://gitcode.com/gh_mirrors/il/illustrator-scripts 还在为Adobe Illustrator中重复繁琐的图案填充而烦恼吗&…

作者头像 李华
网站建设 2026/4/30 13:37:21

TV Bro电视浏览器终极指南:用遥控器轻松掌控大屏上网体验

TV Bro电视浏览器终极指南&#xff1a;用遥控器轻松掌控大屏上网体验 【免费下载链接】tv-bro Simple web browser for android optimized to use with TV remote 项目地址: https://gitcode.com/gh_mirrors/tv/tv-bro 想在智能电视上畅游互联网世界&#xff0c;却苦于遥…

作者头像 李华
网站建设 2026/4/30 13:36:41

Windows 11系统瘦身终极指南:使用Win11Debloat释放系统潜能

Windows 11系统瘦身终极指南&#xff1a;使用Win11Debloat释放系统潜能 【免费下载链接】Win11Debloat A simple, lightweight PowerShell script that allows you to remove pre-installed apps, disable telemetry, as well as perform various other changes to declutter a…

作者头像 李华
网站建设 2026/4/30 13:34:04

2026最权威的五大降重复率方案推荐

Ai论文网站排名&#xff08;开题报告、文献综述、降aigc率、降重综合对比&#xff09; TOP1. 千笔AI TOP2. aipasspaper TOP3. 清北论文 TOP4. 豆包 TOP5. kimi TOP6. deepseek 将文本的 AI 生成痕迹有效降低&#xff0c;可依照以下五项严谨步骤&#xff1a;首先&#xf…

作者头像 李华
网站建设 2026/4/30 13:31:02

批发零售业缺客源?AI 精准对接上下游,获取批量采购询盘

行业痛点分析在当前的批发零售行业中&#xff0c;企业面临着严峻的获客挑战。根据市场调研数据显示&#xff0c;超过60%的批发零售企业在过去一年中经历了客户流失率上升的问题&#xff0c;而其中40%的企业表示新客户的获取成本显著增加。这一现象不仅影响了企业的短期盈利能力…

作者头像 李华
网站建设 2026/4/30 13:22:51

技术破壁:在Windows原生运行安卓应用的创新方案

技术破壁&#xff1a;在Windows原生运行安卓应用的创新方案 【免费下载链接】APK-Installer An Android Application Installer for Windows 项目地址: https://gitcode.com/GitHub_Trending/ap/APK-Installer 还在为Windows上运行安卓应用而烦恼吗&#xff1f;传统模拟…

作者头像 李华