單例傳值

來源:互聯網
上載者:User

標籤:singleton   控制項   iphone   導航   介面   


單例傳值 ------- 如果頁面之間相隔很多,要進行傳值,將值儲存到第三方,將第三方設定為單例模式

代碼如下:

<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


單例傳值

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.