IOS各种传值之一(对象传值)

1:在不同的controllerview之间通过对象进行传值

  //传值方向 从A到B

  //A控制器

 

界面:

//

//  ViewController.h

//  Index

//

 

 

#import <UIKit/UIKit.h>

#import "Person.h"

 

@interface ViewController : UIViewController

 

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

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

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

 

@end

 

 

//

//  ViewController.m

//  Index

//

 

#import "ViewController.h"

#import "BController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

- (IBAction)pushBB:(id)sender {

    BController *bc=[[BController alloc]initWithNibName:@"Bcontroller" bundle:[NSBundle mainBundle]];

 

    Person *person=[[Person alloc]init];

    person.name=self.name.text;

    bc.bsex.text=self.sex.text;

    

    bc.person=person;

    

    [self presentViewController:bc animated:YES completion:nil];

}

 

 

//B控制器

#import "ViewController.h"

#import "Person.h"

 

@interface BController : ViewController

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

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

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

@property (nonatomic,retain)Person *person;

 

 

 

@end

 

 

 

 

#import "BController.h"

 

@interface BController ()

 

@end

 

@implementation BController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    self.bname.text=self.person.name;

    NSLog(@"%@",self.bsex.text);

    

    // Do any additional setup after loading the view.

}

 

- (IBAction)popBB:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];

}

 

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

posted on 2015-04-17 23:50  varliny  阅读(320)  评论(0)    收藏  举报

导航