【iOS】多界面传值

前言

近期的仿写中大量运用了多界面传值,下面写一篇文章来进行总结学习的几种传值方法,记录一下最近这段时间学习。

属性传值

属性传值是一种从前向后传值的方法,在A视图控制器中创建一个B视图控制器的实例,通过对B视图控制器赋值跳转B视图控制器将值传递给B视图控制器。
A视图控制器的接口部分:

#import <UIKit/UIKit.h>
#import "VC1.h"
@interface ViewController : UIViewController

@property (nonatomic, strong) UILabel* label;
@property (nonatomic, strong) UIButton* btn;
@property (nonatomic, strong) VC1* vc;

@end

A视图控制器实现部分:

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.label = [[UILabel alloc] init];
    self.label.frame = CGRectMake(100, 200, 200, 80);
    self.label.tintColor = [UIColor blackColor];
    [self.view addSubview:self.label];
        
    self.btn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.btn.frame = CGRectMake(100, 400, 100, 80);
    [self.btn setTitle:@"B视图" forState:UIControlStateNormal];
    [self.btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    [self.vc addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
}

-(void)press
{

    NSString* arr = @"这是从A视图控制器传来的值";
    VC1* vc1 = [[VC1 alloc] init];
    vc1.arr = arr;
    [self presentViewController:vc1 animated:YES completion:nil];
}

B视图控制器接口部分

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface VC1 : UIViewController

@property (nonatomic, assign) NSString* arr;
@end

NS_ASSUME_NONNULL_END

B视图控制器实现部分:

#import "VC1.h"

@interface VC1 ()

@end

@implementation VC1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 300, 50)];
    label.text = self.arr;
    [self.view addSubview:label];
}
@interface VCxiangqing : UIViewController <NSURLSessionDelegate, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
//
@property (nonatomic, strong) id<cityIDDelegate> delegate;

@end

@end

这就可以把A视图控制器中的值传递给B视图控制器。

协议传值

协议传值是一种从后向前传值的方法,在B视图控制器中定义一个协议,再在A视图控制器中设置代理,通过B协议的方法,将值传递给A视图控制器,

下面用【iOS】APP仿写——天气预报中一段代码展示,这里是将要添加的城市的ID传给主界面,从而主界面进行刷新页面。

//详情界面创建一个协议
@protocol cityIDDelegate <NSObject>
//添加方法
- (void)cityID: (NSString*_Nonnull) cityID andName: (NSString* _Nonnull) name;
 
@end

@interface VCxiangqing : UIViewController <NSURLSessionDelegate, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
//设置代理者属性
@property (nonatomic, strong) id<cityIDDelegate> delegate;
@end

实现部分:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经添加该城市天气至首页" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
        //设置代理的对象来执行协议方法
        [self.delegate cityID:self.CityID andName:self.name];
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alert addAction:cofirm];
    [self presentViewController:alert animated:YES completion:nil];

首页

//添加协议
@interface VCShouye : UIViewController <UITableViewDelegate, UITableViewDataSource, NSURLSessionDataDelegate, UISearchControllerDelegate, UISearchResultsUpdating, NSURLSessionDelegate, cityIDDelegate>

实现部分:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    i
        self.search.searchBar.text = @"";
//        [self.search.searchBar resignFirstResponder];
        NSLog(@"%@", self.arrCityID);
        VCxiangqing* vc = [[VCxiangqing alloc] init];
        vc.CityID = self.arrCityID[indexPath.row];
        NSString* str = [self.arrWeNeed[indexPath.row] substringWithRange:NSMakeRange(0, 2)];
        vc.name = str;//
        vc.hadCity = [_hadCity mutableCopy];
        //设置vc的代理是这个视图控制器
        vc.delegate = self;
        [self.search setActive:NO];
        [self.navigationController pushViewController:vc animated:YES];
}
///实现协议的方法,从而获取到传过来的ID
-(void) cityID:(NSString *)cityID andName:(NSString *)name
{
    
//
    [self.hadCity addObject:cityID];
    [self.cityName addObject:name];
    [self pleaseURL:cityID];
    
//    [self.tableview reloadData];
}

这个是我在博客天气预报中实现添加后传回城市ID进行网络申请从而刷新首页tableview的方法,这里可以看看我另一篇博客天气预报中的搜索添加部分。

通知传值

通知传值通常我们会在跨页面传值中进行时候,或者可以在同一级的视图控制器中进行传值的方法,这里我借助我另一篇博客——
【iOS】APP仿写——网易云音乐
中的夜间模式来进行讲解。
下面通过代码解释:

-(void) pressSwitch: (UISwitch*) sw
{
    self.switchon = sw.isOn;
    BOOL A = self.switchon;
    NSDictionary* dict = @{@"switch":@(A)};
    [[NSNotificationCenter defaultCenter] postNotificationName:@"string" object:nil userInfo:dict];
    //postNotificationName::一个NSString对象,表示要发送的通知的名称。通常使用唯一的字符串来标识通知。
    //object::一个id类型的对象,表示通知的发送者。在这里,将其设置为nil表示没有特定的发送者。
    //userInfo::一个NSDictionary对象,表示通知的附加信息。可以在这里传递一些额外的数据给观察者。
    if(self.switchon) {
        [self.tableView reloadData];
        self.tableView.backgroundColor = [UIColor blackColor];
        self.tabBarController.tabBar.backgroundColor = [UIColor darkGrayColor];
        self.tabBarController.tabBar.barTintColor = [UIColor darkGrayColor];
        self.tabBarController.tabBar.tintColor = [UIColor grayColor];
        
    } else {
        UIColor* wechat = [UIColor colorWithRed:(CGFloat)0xF7/255.0 green:(CGFloat)0xF7/255.0 blue:(CGFloat)0xF7/255.0 alpha:1.0];
        self.tableView.backgroundColor = wechat;
        self.tabBarController.tabBar.backgroundColor = wechat;
        self.tabBarController.tabBar.barTintColor = wechat;
        self.tabBarController.tabBar.tintColor = [UIColor grayColor];
        [self.tableView reloadData];
    }
}

这里我传递的是一个BOOL类型的值,现将这个值放入一个字典中,将字典传递给其他页面,在取出字典中储存的值,对其他页面进行是否夜间模式的判断。 注意:通知的名称要用唯一的字符串标识通知

接收的视图控制器实现部分:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 80) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //self.tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    self.image123 = [UIImage imageNamed:@"头像1.JPG"];
    UIImage *image = [[UIImage imageNamed:@"云朵.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem* left = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(pressleft)];
    self.navigationItem.leftBarButtonItem = left;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[MyCell8 class] forCellReuseIdentifier:@"mycell8"];
    [self.tableView registerClass:[Mycell9 class] forCellReuseIdentifier:@"mycell9"];
    [self.tableView registerClass:[MyCell10 class] forCellReuseIdentifier:@"mycell10"];
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ChangeCell:) name:@"string" object:nil];
    //addObserver::一个id类型的对象,表示要注册为观察者的对象。在这里,self表示当前对象(通常是视图控制器)。
    //selector::一个选择器(Selector),表示接收到通知时要执行的方法。在这里,@selector(ChangeCell:)表示要执行名为ChangeCell:的方法。
    //name::一个NSString对象,表示要注册的通知的名称。通常使用唯一的字符串来标识通知。
    //object::一个id类型的对象,表示发送通知的对象。在这里,将其设置为nil表示接收来自任何对象的通知。
}
//响应接收到的名为"string"的通知,并处理通知中传递的附加信息。
-(void) ChangeCell:(NSNotification*) sender
{
    //取出字典中的内容,并赋值给self.swtichOn
    self.swtichOn = [sender.userInfo[@"switch"]boolValue];
    [self changcolor];
}
- (void)dealloc {
    //当不再需要时,移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"string" object:nil];
 }

注意:注册通知的名称要和发送通知的字符串相同

Block传值

Block传值是一种从后向前的传值方式,他相较于协议传值来说,更为简便。将一个代码块封装,我们可以在里面进行操作,进行传值。

B接口部分

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
//定义Block:首先,我们需要定义一个Block,并指定其参数和返回值类型。
typedef void(^blockvc)(NSString* str);

@interface VCScroll : UIViewController

@property (nonatomic, strong) blockvc Block;

@end

B实现部分:

#import "VCScroll.h"

@interface VCScroll ()

@end

@implementation VCScroll

- (void)viewDidLoad {
    [super viewDidLoad];
    //将传递的值放入Block块
    self.Block(@"block传值");
    UIButton*b = [[UIButton alloc] init];
    b.frame = CGRectMake(100, 200, 50, 50);
    [self.view addSubview:b];
    [b addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    b.backgroundColor = [UIColor blueColor];
}

-(void) press
{
    [self.navigationController popViewControllerAnimated:YES];
}

A实现部分

#import "ViewController.h"
#import "VCScroll.h"
@interface ViewController ()
@property (nonatomic, strong) UILabel* t;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.t = [[UILabel alloc] init];
    
    self.t.frame = CGRectMake(100, 300, 200, 100);
    [self.view addSubview:self.t];
    self.t.backgroundColor = [UIColor redColor];
    UIButton*b = [[UIButton alloc] init];
    b.frame = CGRectMake(100, 200, 50, 50);
    [self.view addSubview:b];
    [b addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    b.backgroundColor = [UIColor blueColor];
}

-(void) press
{
    VCScroll* vc = [[VCScroll alloc] init];
    //调用Block块
    vc.Block = ^(NSString* str) {
        self.t.text = str;
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end

效果图
在这里插入图片描述

KVO传值

KVO(Key-Value Observing)是Objective-C中一种观察者模式的实现,用于观察对象属性值的变化,并在变化发生时通知观察者。

#import <UIKit/UIKit.h>
#import "VCScroll.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) VCScroll* vc;
@end
#import "ViewController.h"
#import "VCScroll.h"
@interface ViewController ()
@property (nonatomic, strong) UILabel* t;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.t = [[UILabel alloc] init];
    
    self.t.frame = CGRectMake(100, 300, 200, 100);
    [self.view addSubview:self.t];
    self.t.backgroundColor = [UIColor redColor];
    UIButton*b = [[UIButton alloc] init];
    b.frame = CGRectMake(100, 200, 50, 50);
    [self.view addSubview:b];
    [b addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    b.backgroundColor = [UIColor blueColor];
    
    self.vc = [[VCScroll alloc] init];
    
    [self.vc addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial context:nil];
    //self 是当前对象,通常是观察者自身。
    //forKeyPath: 是一个字符串,表示要观察的属性的键路径。在这个例子中,我们观察的是 self.vc 对象的 age 属性。
    //options: 是一个枚举值,用于指定观察选项。在这里,我们使用了 NSKeyValueObservingOptionNew 和 NSKeyValueObservingOptionInitial,表示我们希望在观察开始时和属性值变化时都接收通知。
    //context: 是一个指针,用于传递上下文信息。在这里,我们将其设置为 nil。
}

-(void) press
{
    self.vc.age++;
    
}
//KVO观察者方法的实现
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    self.t.text = [NSString stringWithFormat:@"显示:%d", self.vc.age] ;
}
//移除观察者
- (void)dealloc
{
    [self.vc removeObserver:self forKeyPath:@"key"];
}

@end

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^blockvc)(NSString* str);

@interface VCScroll : UIViewController

@property int age;

@end

NS_ASSUME_NONNULL_END

效果图:
在这里插入图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值