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);
}