Delegate 用于简单的页面传值方法

这篇博客详细介绍了如何在iOS应用中使用Delegate来实现页面间的简单传值。通过创建一个协议和代理方法,从页面B返回时将数据传递回页面A。文章通过实例代码展示了如何在页面A中声明和实现代理,以及在页面B中调用代理方法并将数据传回。

//有页面A和页面B

//页面A跳转到B 

// 页面B 返回 并传值给A

//A页面跳转B 不传值仅跳转

A页面 代码

//.h

#import <UIKit/UIKit.h>


@interface ViewControllerA : UIViewController



@end


//.m

#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


页面B

//.h

#import <UIKit/UIKit.h>

//创建协议

@protocol ViewControllerBDelegate <NSObject>

@required

//协议方法即传值方法

-(void)addName:(NSString *)name;


@end

@interface ViewControllerB : UIViewController

//声明代理

@property (nonatomic,weak) id<ViewControllerBDelegate> Delegate;



@end


//.m

#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

//此代码要把storyboard的ViewController 改成ViewControllerA 才能显示
//或者也可以在AppDelegate中写如下代码指定 起始页面 但是要删除设置里的启动文件Main 
#import “ViewControllerA”

- (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;

}








评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值