单例传值
單例傳值 ------- 如果頁面之間相隔很多,要進(jìn)行傳值,將值保存到第三方,將第三方設(shè)置為單例模式
代碼如下:
[objc]?view plaincopy
<pre?name="code"?class="objc">#import?<Foundation/Foundation.h>??
??
@interface?Singleton?:?NSObject??
+?(Singleton?*)shareSingleton;??
@property?(nonatomic,copy)NSString?*?text;??
@end??
[objc]?view plaincopy
#import?"Singleton.h"??
??
@implementation?Singleton??
??
static?Singleton?*?singleton?=?nil;??
+?(Singleton?*)shareSingleton??
{??
????@synchronized(self){??
????????if?(singleton?==?nil)?{??
????????????singleton?=?[[Singleton?alloc]init];??
????????}??
????}??
????return?singleton;??
}??
@end??
[objc]?view plaincopy
#import?"FirstViewController.h"??
#import?"SecondViewController.h"??
#import?"UIButton+Create.h"??
#import?"Singleton.h"??
@interface?FirstViewController?()??
{??
????UITextField?*?_textField;//創(chuàng)建一個輸入框??
}??
@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.在第一個界面創(chuàng)建一個輸入框?
?????*???
?????*/??
????_textField?=?[[UITextField?alloc]initWithFrame:CGRectMake(50,?80,?200,?30)];??
????_textField.borderStyle?=?UITextBorderStyleRoundedRect;??
????[self.view?addSubview:_textField];??
??????
??????
????/**?
?????*??1.創(chuàng)建一個UIButton,?
?????*??2.并添加響應(yīng)事件,從首頁跳轉(zhuǎn)到第二個頁面.?
?????*/??
????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.從而實現(xiàn)把首頁輸入框輸入的字符串,傳到第二頁的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??
[objc]?view plaincopy
#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.在第二個界面創(chuàng)建一個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??
轉(zhuǎn)載于:https://blog.51cto.com/9453125/1561053
總結(jié)
- 上一篇: jquery高版本全选与全部选无法正常工
- 下一篇: leveldb demo