頁面之間傳值(單例傳值)(自己使用),頁面

來源:互聯網
上載者:User

頁面之間傳值(單例傳值)(自己使用),頁面

首先建立2個新介面 , 然後建立一個類,如

 

然後在AppDeleate.h

 

#import <UIKit/UIKit.h>#import "FirstViewController.h"@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end

然後在AppDeleate.m中

#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    FirstViewController *firstvc=[[FirstViewController alloc]init];    self.window.rootViewController=firstvc;    return YES;}

 然後進入AppStatus.h

 

#import <Foundation/Foundation.h>@interface AppStatus : NSObject@property(strong ,nonatomic)NSString *contextStr;+(AppStatus *)shareInstance;@end

 AppStatus.m

#import "AppStatus.h"@implementation AppStatus@synthesize contextStr = _contextStr;static AppStatus *_instance = nil;+(AppStatus *)shareInstance{    if (_instance==nil) {        _instance=[[super alloc]init];    }    return _instance;}-(id)init{    if (self=[super init]) {            }    return self;}@end

 

FirstViewController.h中

 

#import <UIKit/UIKit.h>#import "SecondViewController.h"#import "AppStatus.h"@interface FirstViewController : UIViewController<UITextFieldDelegate>@property(strong,nonatomic) UITextField *text1;@property(strong,nonatomic) UIButton *btn1;@end

 FirstViewController.m中

#import "FirstViewController.h"@interface FirstViewController ()@end@implementation FirstViewController- (void)viewDidLoad {    [super viewDidLoad];        self.view.backgroundColor=[UIColor greenColor];        self.text1=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 60)];        self.text1.borderStyle=1;        [self.view addSubview:self.text1];        self.text1.delegate=self;    self.btn1=[[UIButton alloc]initWithFrame:CGRectMake(130, 300, 150, 60)];        [self.btn1 setTitle:@"下一頁" forState:0];        [self.btn1 setTitleColor:[UIColor blackColor ] forState:0];        [self.btn1 addTarget:self action:@selector(nextpage) forControlEvents:UIControlEventTouchUpInside];        [self.view addSubview:self.btn1];        }-(void)nextpage{    [AppStatus shareInstance].contextStr=self.text1.text;    SecondViewController *secondvc=[[SecondViewController alloc]init];        [self presentViewController:secondvc animated:YES completion:^{        NSLog(@"ok");    }];}-(BOOL)textFieldShouldReturn:(UITextField *)textField{    if ([textField isFirstResponder]) {        [textField resignFirstResponder];    }                return YES;}-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];               self.text1.text=[AppStatus shareInstance].contextStr;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];

 

SecondViewController.h

#import <UIKit/UIKit.h>#import "AppStatus.h"@interface SecondViewController : UIViewController<UITextFieldDelegate>@property(strong,nonatomic) UITextField *text2;@property(strong,nonatomic) UIButton *btn2;@end

 SecondViewController.m

#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor=[UIColor redColor];        self.text2=[[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 60)];        self.text2.borderStyle=1;        [self.view addSubview:self.text2];        self.text2.delegate=self;        self.text2.text=[AppStatus shareInstance].contextStr;        self.btn2=[[UIButton alloc]initWithFrame:CGRectMake(130, 300, 150, 60)];        [self.btn2 setTitle:@"返回" forState:0];        [self.btn2 setTitleColor:[UIColor colorWithRed:0.038 green:0.249 blue:1.000 alpha:1.000] forState:0];        [self.btn2 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:self.btn2];}-(void)back{    [AppStatus shareInstance].contextStr=self.text2.text;     [self dismissViewControllerAnimated:YES completion:nil];}-(BOOL)textFieldShouldReturn:(UITextField *)textField{    if ([textField isFirstResponder]) {        [textField resignFirstResponder];    }        [self dismissViewControllerAnimated:YES completion:nil];            return YES;}-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];                self.text2.text=[AppStatus shareInstance].contextStr;}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.