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];
@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];
#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 ;
}