iOS传值一属性传值,代理传值

本文介绍了iOS开发中两种常见的传值方式:属性传递和使用代理。通过具体实例展示了如何在两个视图控制器间利用这两种方式进行数据交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.属性传值

属相传值一般从前向后,即从A到B,要传什么类型值,就在B页面定义什么样的属性

如要将一个字符串从A传到B,就在B页面设置以下属相

@property(nonatomic,retain) NSString *str ;//接受的值

在A控制器跳转方法内实现

- (void)showSecondView:(UIButton*)btn{
SecondViewController *VC = [[SecondViewController alloc] init];
VC.str =@"我是属性传值";
[self presentViewController:VC animated:YES completion:nil];
}
属性传值,比较简单,也非常常用

2.delegate


 在页面B设置协议及方法          

//SecondViewController.h
//设置代理@protocol secondViewDelegate//代理方法(为防止循环引用用weak修饰)
-(void)showMessage:(NSString *)messageStr;
@end
//SecondViewController.h
@interface SecondViewController : UIViewController
@property (nonatomic, weak)id<secondViewDelegate> delegate;
@end
   调用
//SecondViewController.m
- (void)backFirst:(UIButton*)btn{
[self.delegate showMessage:@"我是代理传值"];
[self dismissViewControllerAnimated:YES completion:nil];

}
在页面A显示  
-(void)showName:(NSString *)messageStr{
NSLong(@"%@",messageStr);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值