单例传值 ------- 如果页面之间相隔很多,要进行传值,将值保存到第三方,将第三方设置为单例模式

代码如下:

[objc] view plaincopy

  1. <pre name="code" class="objc">#import <Foundation/Foundation.h>  

  2.   

  3. @interface Singleton : NSObject  

  4. + (Singleton *)shareSingleton;  

  5. @property (nonatomic,copy)NSString * text;  

  6. @end  






[objc] view plaincopy

  1. #import "Singleton.h"  

  2.   

  3. @implementation Singleton  

  4.   

  5. static Singleton * singleton = nil;  

  6. + (Singleton *)shareSingleton  

  7. {  

  8.     @synchronized(self){  

  9.         if (singleton == nil) {  

  10.             singleton = [[Singleton alloc]init];  

  11.         }  

  12.     }  

  13.     return singleton;  

  14. }  

  15. @end  



[objc] view plaincopy

  1. #import "FirstViewController.h"  

  2. #import "SecondViewController.h"  

  3. #import "UIButton+Create.h"  

  4. #import "Singleton.h"  

  5. @interface FirstViewController ()  

  6. {  

  7.     UITextField * _textField;//创建一个输入框  

  8. }  

  9. @end  

  10.   

  11. @implementation FirstViewController  

  12. - (void)dealloc  

  13. {  

  14.     [_textField release];  

  15.     [super dealloc];  

  16. }  

  17. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  

  18. {  

  19.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  

  20.     if (self) {  

  21.         // Custom initialization  

  22.     }  

  23.     return self;  

  24. }  

  25.   

  26. - (void)viewDidLoad  

  27. {  

  28.     [super viewDidLoad];  

  29.       

  30.       

  31.     self.view.backgroundColor = [UIColor redColor];  

  32.     self.navigationItem.title = @"首页";  

  33.     /** 

  34.      *  1.在第一个界面创建一个输入框 

  35.      *   

  36.      */  

  37.     _textField = [[UITextField alloc]initWithFrame:CGRectMake(508020030)];  

  38.     _textField.borderStyle = UITextBorderStyleRoundedRect;  

  39.     [self.view addSubview:_textField];  

  40.       

  41.       

  42.     /** 

  43.      *  1.创建一个UIButton, 

  44.      *  2.并添加响应事件,从首页跳转到第二个页面. 

  45.      */  

  46.     UIButton * button = [UIButton systemButtonWithFrame:CGRectMake(1001205050) title:@"Push" target:self action:@selector(didClickButtonAction)];  

  47.     [self.view addSubview:button];  

  48.       

  49.       

  50.     // Do any additional setup after loading the view.  

  51. }  

  52.   

  53. - (void)didClickButtonAction  

  54. {  

  55.       

  56.     /** 

  57.      *  1.用push的方法推出下一个页面 

  58.      *  2.把首页输入框输入的字符串,通过单例来接收 

  59.      *  3.从而实现把首页输入框输入的字符串,传到第二页的UILabel上. 

  60.      */  

  61.     SecondViewController * secondVC = [[SecondViewController alloc]init];  

  62.     [Singleton shareSingleton].text = _textField.text;  

  63.     [self.navigationController pushViewController:secondVC animated:YES];  

  64.     [secondVC release];  

  65. }  

  66.   

  67.   

  68. - (void)didReceiveMemoryWarning  

  69. {  

  70.     [super didReceiveMemoryWarning];  

  71.     // Dispose of any resources that can be recreated.  

  72. }  

  73.   

  74. @end  


单例传值

[objc] view plaincopy

  1. #import "SecondViewController.h"  

  2. #import "Singleton.h"  

  3. @interface SecondViewController ()  

  4.   

  5. @end  

  6. @implementation SecondViewController  

  7.   

  8. - (void)dealloc  

  9. {  

  10.     [_label release];  

  11.     [super dealloc];  

  12. }  

  13. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  

  14. {  

  15.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  

  16.     if (self) {  

  17.         // Custom initialization  

  18.     }  

  19.     return self;  

  20. }  

  21.   

  22. - (void)viewDidLoad  

  23. {  

  24.     [super viewDidLoad];  

  25.     self.view.backgroundColor = [UIColor orangeColor];  

  26.     self.navigationItem.title = @"第二页";  

  27.     /** 

  28.      *  1.在第二个界面创建一个UILabel 

  29.      *  2.把首页输入框输入的字符串,通过单例类的属性NSString * text接收 

  30.      *  3.然后通过赋值给UILabel 

  31.      */  

  32.     _label = [[UILabel alloc]initWithFrame:CGRectMake(508020030)];  

  33.     _label.backgroundColor = [UIColor greenColor];  

  34.       

  35.     _label.text = [Singleton shareSingleton].text;  

  36.       

  37.     [self.view addSubview:_label];  

  38.       

  39.     // Do any additional setup after loading the view.  

  40. }  

  41.   

  42. - (void)didReceiveMemoryWarning  

  43. {  

  44.     [super didReceiveMemoryWarning];  

  45.     // Dispose of any resources that can be recreated.  

  46. }  

  47.   

  48. @end  


单例传值