iOS 传值方式一

iOS的传值方式一:
前往后传值**《》《》《》《》通过属性传值

@interface NewsController : UIViewController 
从 NewsController 往后传值

#pragma mark  点击cell 传递值 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     // QianChuanHouController  是后面接收数据的页面
    QianChuanHouController *controll = [[QianChuanHouController alloc]init];
    UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:controll];

   NewsModel *models = self.dataArray[indexPath.row];
    controll.model = models;
        [self presentViewController:na animated:YES completion:nil];
  NSLog(@"%ld", indexPath.row);
}
// 这个是 接收页面 
@protocol QianChuanZhiDelegate <NSObject>
@property (nonatomic, strong)NewsModel *model;
// 这样就可以把 数据从前一个页面传给后一个页面

传值方式二:<><><><><><><> 后面往前传 通过代理传值
在后面要传值的页面 设置代理 并且设置方法
在前面要接受值得页面 遵循代理实现代理方法

//  后面往前传  需要用到代理
 在 QianChuanHouController 控制器里面设代理

 // 声明代理
 @protocol QianChuanZhiDelegate <NSObject>

 //声明代理方法
   @optional 
- (void)ViewController:(NSString *)string;
@end
@interface QianChuanHouController : UIViewController
@property (nonatomic, strong)NewsModel *model;
@property (nonatomic, strong)id <QianChuanZhiDelegate>delegate;
 ```
// 实现代理方法
- (void)actionss {
    // 实现代理方法
    [self.delegate ViewController:self.model.name];

    [self dismissViewControllerAnimated:YES completion:nil];
}
// 在接接收的页面遵循代理并且实现方法
@interface NewsController ()<QianChuanZhiDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QianChuanHouController *controll = [[QianChuanHouController alloc]init];
    controll.delegate = self;

    UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:controll];

   NewsModel *models = self.dataArray[indexPath.row];
    controll.model = models;

    [self presentViewController:na animated:YES completion:nil];


    NSLog(@"%ld", indexPath.row);
}
//  实现代理方法
- (void)ViewController:(NSString *)string
{
    NSLog(@"代理传值   %@", string);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值