SceondView作为上个视图,push到下一个视图FirstView,
在push的同时把下一个视图(FirstView中ViewDidLoad中的数据) 传到 上一个视图(SecondView)
////////////////////////////// MYDelegate.h ///////////////
#import <Foundation/Foundation.h>
@protocol myDelegate <NSObject> //协议名称
-(void)passValue:(NSString*)value; //协议方法:(数据类型*)数据名称
@end
/////////////////////////////// FirstViewController.h ///////////////
#import <UIKit/UIKit.h>
#import "MYdelegate.h"
@interface FirstViewController : UIViewController
@property (nonatomic ,retain) id<myDelegate> theDelegate; //theDelegate找到代理人,我接受协议,我来干活
@end
//
////////////////////////////// SecondViewController.h ////////////////////////////
#import <UIKit/UIKit.h>
#import "MYdelegate.h"
#import "FirstViewController.h"
@interface SecondViewController : UIViewController<myDelegate> //我要用这个协议,哥有钱,谁来干活
@end
//
////////////////////////////// SecondViewController.m ////////////////////////////
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton*button5=[[UIButton alloc]initWithFrame:CGRectMake(15, 150, 50, 30)] ;
[button5 setBackgroundColor:[UIColor redColor]];
[button5 setTitle:@"to1" forState:UIControlStateNormal];
[button5 addTarget:self action:@selector(GOEEE) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button5];
}
//在push时,实现协议方法
-(void)GOEEE
{
FirstViewController*view=[[FirstViewController alloc]init];
view.theDelegate=self; //自己作为代理,出去找人
[self.navigationController pushViewController: view animated:YES];
}
//////定义协议方法
- (void)passValue:(NSString *)value{
NSLog(@"%@",value);
}
///////////////////////////// FirstViewController.m ///////////////
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.theDelegate passValue:@"dshfahfd"]; //需要传的内容
UIButton*button6=[[UIButton alloc]initWithFrame:CGRectMake(105, 150, 50, 30)] ;
[button6 setBackgroundColor:[UIColor redColor]];
[button6 setTitle:@"to2" forState:UIControlStateNormal];
[button6 addTarget:self action:@selector(GOFFF) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button6];
}
-(void)GOFFF
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
///////////////////////////// FirstViewController.m ///////////////
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.theDelegate passValue:@"dshfahfd"]; //需要传的内容
UIButton*button6=[[UIButton alloc]initWithFrame:CGRectMake(105, 150, 50, 30)] ;
[button6 setBackgroundColor:[UIColor redColor]];
[button6 setTitle:@"to2" forState:UIControlStateNormal];
[button6 addTarget:self action:@selector(GOFFF) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button6];
}
-(void)GOFFF
{
[self.navigationController popViewControllerAnimated:YES];
}
@end
本文介绍了一种在iOS应用中从SecondView推送到FirstView并反向传递数据的方法。通过定义一个代理协议MYDelegate,使得FirstView可以将数据通过该协议发送回SecondView。涉及的代码展示了如何设置和使用这个代理机制。

3478

被折叠的 条评论
为什么被折叠?



