216,页面之间的传值(二)

216,页面之间的传值(二)_第1张图片

AppDelegate.h:

#import

@class OneViewController;


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;

@property (nonatomic,strong) OneViewController *oneView;



@end


AppDelegate.m:

#import "AppDelegate.h"

#import "OneViewController.h"


@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //创建window

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    //创建第一个界面

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"OneViewController" bundle:nil];

    self.oneView = [storyboard instantiateInitialViewController];

    //创建一个UINavigationController

    UINavigationController *navigation = [[UINavigationController alloc] init];

    [navigation pushViewController: self.oneView animated:YES];

    self.window.rootViewController = navigation;

    [self.window makeKeyAndVisible];

    return YES;

}


@end


Person.h:

#import


@interface Person : NSObject


@property (nonatomic,strong) NSString *name;

@property (nonatomic,strong) NSString *age;


- (instancetype)initWithName:(NSString  *)name WithAge:(NSString *)age;

+ (instancetype)personWithName:(NSString  *)name WithAge:(NSString *)age;


@end


Person.m:

#import "Person.h"


@implementation Person


- (instancetype)initWithName:(NSString *)name WithAge:(NSString *)age{

    self = [super init];

    if (self) {

        self.name = name;

        self.age = age;

    }

    return self;

}


+ (instancetype)personWithName:(NSString *)name WithAge:(NSString *)age{

    return [[self alloc] initWithName:name WithAge:age];

}


@end


OneViewController.h:

#import

@class Person;

@class OneViewController;


@protocol OneViewControllerDelegate <NSObject>


- (void)oneViewWithSelf:(OneViewController *) oneView WithPerson:(Person *) person;


@end


@interface OneViewController : UIViewController


@property (nonatomic,weak) id<OneViewControllerDelegate> delegate;


@end


OneViewController.m:

#import "OneViewController.h"

#import "TwoViewController.h"

#import "Person.h"



@interface OneViewController ()


@property (weak, nonatomic) IBOutlet UITextField *name;

@property (weak, nonatomic) IBOutlet UITextField *age;


- (IBAction)sendValue:(UIButton *)sender;

@end


@implementation OneViewController



- (IBAction)sendValue:(UIButton *)sender {

    TwoViewController *twoView = [TwoViewController twoView];

    self.delegate = twoView;

    [self.navigationController pushViewController:twoView animated:YES];

    Person *person = [Person personWithName:self.name.text WithAge:self.age.text];

    if ([self.delegate respondsToSelector:@selector(oneViewWithSelf:WithPerson:)]) {

        

       [self.delegate oneViewWithSelf:self WithPerson:person];

    }

}

@end


TwoViewController.h:

#import


@interface TwoViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *name;

@property (weak, nonatomic) IBOutlet UILabel *age;


+ (instancetype)twoView;


@end


TwoViewController.m:

#import "TwoViewController.h"

#import "OneViewController.h"

#import "Person.h"


@interface TwoViewController () <OneViewControllerDelegate>


@property (nonatomic,strong) Person *personInfo;


@end


@implementation TwoViewController


+ (instancetype)twoView{

    return [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];

}


- (void)viewDidLoad{

    self.name.text = self.personInfo.name;

    self.age.text = self.personInfo.age;

}


- (void)oneViewWithSelf:(OneViewController *)oneView WithPerson:(Person *)person{

    self.personInfo = person;

}


@end


注:另外一种方法,可以采用全局变量来存值与取值,共享到页面


你可能感兴趣的:(IOS之UI)