Value Transfer between pages (single-case value transfer) (for your own use), page

Source: Internet
Author: User

Value Transfer between pages (single-case value transfer) (for your own use), page

First create two new interfaces and then create a class, as shown in figure

 

Then in AppDeleate. h

 

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

Then in 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;}

Then enter 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

 

In 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

In 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: @ "next page" 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 terminated instance]. 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 attached instance]. contextStr;}-(void) didreceivemorywarning {[super didreceivemorywarning];

 

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 attached instance]. contextStr; self. btn2 = [[UIButton alloc] initWithFrame: CGRectMake (130,300,150, 60)]; [self. btn2 setTitle: @ "Returned" 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 reset instance]. contextStr = self. text2.text; [self defined: 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 attached instance]. contextStr;}-(void) didreceivemorywarning {[super didreceivemorywarning];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.