iOS 界面间的传值 属性传值 代理传值

1.属性传值

从BViewController传到AViewController 

BViewController.m

#import "AViewController.h"

#import "BViewController.h"

在需要回传的地方传值

AViewController *a = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];

a.contactId = 需要传的值;

 [self.navigationController popToViewController:a animated:true];

 

AViewController.h

@property (retain, nonatomic) NSString *contactId;

@property (strong, nonatomic) UITextField *contactIdField;

 

AViewController.m

#import "AViewController.h"

#import "BViewController.h"

在点击跳到下一个页面的方法中

    BViewController *b = [[BViewController alloc] init];

    [b release];

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

-(void)viewWillAppear:(BOOL)animated

{

    self.contactIdField.text = self.contactId ;

}

 

2.代理传值1(yf)

从BViewController传到AViewController 

BViewController.h

#import <UIKit/UIKit.h>

@protocol BDelegate <NSObject>

- (void)setIDValue:(NSString *)value;

@end

@interface BViewController : UIViewController

@property(nonatomic, retain) id<BDelegate> delegate; // 注册一个代理

@end

 

BViewController.m

#import "BViewController.h"

在需要回传的地方传值   

    [_delegate setIDValue:需要传的值];

    [self.navigationController popViewControllerAnimated:YES];

 

AViewController.h

@interface AViewController : UIViewController

@property (strong, nonatomic) UITextField *contactIdField;

@end

 

AViewController.m

@interface AViewController ()<IDDelegate> // 遵守协议

@end

在点击跳到下一个页面的方法中

    BViewController *b = [[BViewController alloc] init];

    b.delegate = self;

    [b release];

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

- (void)setIDValue:(NSString *)value // 实现方法

{

    _contactIdField.text = value ;

}

 

3代理传值(jl)

从BViewController传到AViewController

BViewController.h

#import <UIKit/UIKit.h>

@protocol BDelegate <NSObject>

- (void)setIDValue:(NSString *)value;

@end

@interface BViewController : UIViewController

@end

 

BViewController.m

#import "BViewController.h"

@interface BViewController ()

@property(nonatomic, retain) id<BDelegate> delegate; // 注册一个代理

@end

在需要回传的地方传值

    AViewController *a = [self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-2];

    self.delegate = a;   // 这里和AViewController 进行交互,下一句代码就是对应的赋值

    [self.delegate setIDValue:需要传的值];

    [self.navigationController popToViewController:a   animated:true];

AViewController.h

#import "BViewController.h" // 导入协议

@interface AViewController : UIViewController<IDDelegate>// 遵守协议

@property (strong, nonatomic) UITextField *contactIdField;

@end

 

AViewController.m

在点击跳到下一个页面的方法中

    BViewController *b = [[BViewController alloc] init];

    [b release];

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

- (void)setIDValue:(NSString *)value // 实现方法

{

    self.contactIdField.text = value ;

}

 

posted @ 2015-01-23 18:31  浅忆0  阅读(243)  评论(0编辑  收藏  举报