//有页面A和页面B
//页面A跳转到B
// 页面B 返回 并传值给A
//A页面跳转B 不传值仅跳转
A页面 代码
//.h
#import <UIKit/UIKit.h>
@interface ViewControllerA : UIViewController
@end
#import "ViewControllerA.h"
//把界面B的头文件引入
#import "ViewControllerB.h"
//添加代理
@interface ViewControllerA ()<ViewControllerBDelegate>
@property (nonatomic,strong) UILabel *labelShow;
@end
@implementation ViewControllerA
- (void)viewDidLoad {
[super viewDidLoad];
//创建Button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];
//设置button的名字
[button setTitle:@"跳转页面B" forState:UIControlStateNormal];
//设置Button的名字颜色
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//设置button点击事件
[button addTarget:self action:@selector(btnPress) forControlEvents:UIControlEventTouchUpInside];
//将Button显示到页面上
[self.view addSubview:button];
//创建显示传过来值得label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
//添加字体颜色
label.textColor = [UIColor redColor];
//添加属性
self.labelShow = label;
//添加到页面
[self.view addSubview:label];
}
//button点击触发的事件
-(void)btnPress
{
//创建实例
ViewControllerB *vcB = [[ViewControllerB alloc]init];
//页面跳转
[self presentViewController:vcB animated:YES completion:nil];
//声明代理人
vcB.Delegate = self;
}
//实现代理方法
-(void)addName:(NSString *)name
{
self.labelShow.text = name;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
//.h
#import <UIKit/UIKit.h>
//创建协议
@protocol ViewControllerBDelegate <NSObject>
@required
//协议方法即传值方法
-(void)addName:(NSString *)name;
@end
@interface ViewControllerB : UIViewController
//声明代理
@property (nonatomic,weak) id<ViewControllerBDelegate> Delegate;
@end
#import "ViewControllerB.h"
@interface ViewControllerB ()
//在扩展里面设置label属性
@property (nonatomic,strong) UILabel *labelName;
@end
@implementation ViewControllerB
- (void)viewDidLoad {
[super viewDidLoad];
//设置背景色
self.view.backgroundColor = [UIColor whiteColor];
//创建Label
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];
//赋值
label.text = @"Clement";
//添加属性
self.labelName = label;
//在页面添加label控件
[self.view addSubview:label];
//创建button
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];
//设置标题
[btn setTitle:@"返回" forState:UIControlStateNormal];
//设置字体颜色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//添加事件
[btn addTarget:self action:@selector(btnPress) forControlEvents:UIControlEventTouchUpInside];
//页面添加button
[self.view addSubview:btn];
}
//buttond点击事件
-(void)btnPress
{
//调用代理 并传值
[self.Delegate addName:self.labelName.text];
//消除本页面并返回
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
ViewControllerB *vcA = [[ViewControllerB alloc]init];
//指定根控制器
self.window.rootViewController = vcA;
return YES;
}
注意:点就是声明谁是代理人的时候 页面切换在哪里就在哪里声明代理人
即button点击事件切换就要在button点击事件里进行声明Delegate
//沿线segue跳转则是在prepareForSegue 中进行声明
-(void)btnPress
{
//沿线跳转页面
[self performSegueWithIdentifier:@"popB" sender:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
ViewControllerB *vcB = segue.destinationViewController;
vcB.Delegate = self;
}
这篇博客详细介绍了如何在iOS应用中使用Delegate来实现页面间的简单传值。通过创建一个协议和代理方法,从页面B返回时将数据传递回页面A。文章通过实例代码展示了如何在页面A中声明和实现代理,以及在页面B中调用代理方法并将数据传回。
730

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



