单例传值
来源:互联网 发布:空调结构设计知乎 编辑:程序博客网 时间:2024/05/17 00:35
单例传值 ------- 如果页面之间相隔很多,要进行传值,将值保存到第三方,将第三方设置为单例模式
代码如下:
<pre name="code" class="objc">#import <Foundation/Foundation.h>@interface Singleton : NSObject+ (Singleton *)shareSingleton;@property (nonatomic,copy)NSString * text;@end
#import "Singleton.h"@implementation Singletonstatic Singleton * singleton = nil;+ (Singleton *)shareSingleton{ @synchronized(self){ if (singleton == nil) { singleton = [[Singleton alloc]init]; } } return singleton;}@end
#import "FirstViewController.h"#import "SecondViewController.h"#import "UIButton+Create.h"#import "Singleton.h"@interface FirstViewController (){ UITextField * _textField;//创建一个输入框}@end@implementation FirstViewController- (void)dealloc{ [_textField release]; [super dealloc];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; self.navigationItem.title = @"首页"; /** * 1.在第一个界面创建一个输入框 * */ _textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 80, 200, 30)]; _textField.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:_textField]; /** * 1.创建一个UIButton, * 2.并添加响应事件,从首页跳转到第二个页面. */ UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(100, 120, 50, 50) title:@"Push" target:self action:@selector(didClickButtonAction)]; [self.view addSubview:button]; // Do any additional setup after loading the view.}- (void)didClickButtonAction{ /** * 1.用push的方法推出下一个页面 * 2.把首页输入框输入的字符串,通过单例来接收 * 3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上. */ SecondViewController * secondVC = [[SecondViewController alloc]init]; [Singleton shareSingleton].text = _textField.text; [self.navigationController pushViewController:secondVC animated:YES]; [secondVC release];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
#import "SecondViewController.h"#import "Singleton.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)dealloc{ [_label release]; [super dealloc];}- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor orangeColor]; self.navigationItem.title = @"第二页"; /** * 1.在第二个界面创建一个UILabel * 2.把首页输入框输入的字符串,通过单例类的属性NSString * text接收 * 3.然后通过赋值给UILabel */ _label = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 200, 30)]; _label.backgroundColor = [UIColor greenColor]; _label.text = [Singleton shareSingleton].text; [self.view addSubview:_label]; // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
1 0
- 单例传值
- 单例传值
- 单例传值
- iOS 单例传值
- iOS 单例传值
- iOS单例传值
- UI_Singleton(单例传值)
- ios单例传值
- 单例传值 南哥教的
- (学习笔记)单例传值
- 单例传值(三个界面)
- iOS大典之单例传值
- 关于UIApplication单例传值
- ios利用单例传值
- iOS - 单例传值 (一)
- IOS传值之单例传值
- Swift 传值之单例传值
- swift 单例传值及其使用方式
- C#值传递与引用传递
- 暑期个人赛--第一场--E
- 网络编程之一:网络编程的大门
- 网络编程之二:java.net包的Socket和SocketServer
- 网络编程之三:java.net包的DatagramSocket和DatagramPacket
- 单例传值
- 分布式系统之一:分布式系统的挑战
- RPC/RMI之一:RPC简介
- RPC/RMI之二:XMLRPC
- RPC/RMI之三:JSONRPC
- 分布式系统之二:CAP定理
- 分布式系统之三:CAP定理的实践
- Java之一:Serialization(序列化…
- Java之二:工厂模式