2024年最新iOS开发进阶(十八):页面传参(1),2024年最新Web前端面试中常问的MMAP到底是啥东东

总结
  • 对于框架原理只能说个大概,真的深入某一部分具体的代码和实现方式就只能写出一个框架,许多细节注意不到。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

  • 算法方面还是很薄弱,好在面试官都很和蔼可亲,擅长发现人的美哈哈哈…(最好多刷一刷,不然影响你的工资和成功率???)

  • 在投递简历之前,最好通过各种渠道找到公司内部的人,先提前了解业务,也可以帮助后期优秀 offer 的决策。

  • 要勇于说不,对于某些 offer 待遇不满意、业务不喜欢,应该相信自己,不要因为当下没有更好的 offer 而投降,一份工作短则一年长则 N 年,为了幸福生活要慎重选择!!!

第一次跳槽十分忐忑不安,和没毕业的时候开始找工作是一样的感受,真的要相信自己,有条不紊的进行。如果有我能帮忙的地方欢迎随时找我,比如简历修改、内推、最起码,可以把烦心事说一说,人嘛都会有苦恼的~

祝大家都有美好的未来,拿下满意的 offer。

  1. 在NextViewController.h 定义一个属性(本文以字符串为例):
@property (nonatomic,strong) NSString \*strProperty2;

  1. 在ViewController中的点击事件做如下操作:
//属性传值
- (void)btnProperty{
    
    NextViewController \*nextVC = [[NextViewControlleralloc]init];

    nextVC.strProperty2 = @"shq5785";

    [self.navigationController pushViewController:nextVC animated:YES];

}

  1. 这时B控制器已经拿到了A传来的值,只需要在viewDidLoad做个赋值操作即可展现出传过来的字符串了:
//属性传值
self.lblProperty.text =self.strProperty2;

2.2 属性传值B–>A

属性由B到A传值会比A到B稍微复杂一点,不过也是分为三步:

  1. 给A定义一个外部可用属性:
@property (nonatomic,strong) NSString \*strProperty1;

  1. 在B跳转回A的点击事件中找到A控制器并给其属性赋值,这里拿到A控制器就不能跟A–>B那样直接新建一个控制器了,因为A控制器还存在,只是没有显示出来而已;所以应该通过导航栏控制器中获取当前A控制器:
- (void)btnProperty{
    ViewController \*VC =self.navigationController.viewControllers[0];

    VC.strProperty1 = @"iOS";

    [self.navigationController popViewControllerAnimated:YES];

}

  1. 这时A中已经拿到了B传过来的值,但是要显示的话应该在viewWillAppear中处理:
- (void)viewWillAppear:(BOOL)animated{
	// 属性传值
    self.lblProperty.text = self.strProperty1;
}

三、代理传值

3.1 代理传值A–>B

3.1 在A控制器的头文件申明协议:

@protocol ViewControllerDelegate <NSObject>

@optional

- (void)delegateSendToNext:(NSString \* )strDelegate;

@end 

  1. @interface中添加遵循协议的代理属性
@property (nonatomic,assign)id<ViewControllerDelegate> delegate;

  1. 在B的头文件中遵循A的协议;
@interface NextViewController :UIViewController<ViewControllerDelegate>

  1. 在A跳转到B的点击事件中,让B界面成为A的代理,并调用代理方法;
//代理传值

- (void)btnDelegate{
   NextViewController \*nextVC = [[NextViewController alloc]init];

  	self.delegate = nextVC;

	[self.delegate delegateSendToNext:@"shq5785"];

	[self.navigationController pushViewController:nextVC animated:YES];

}

  1. 在B控制器实现A协议中的方法完成传值。
//在B控制器实现A控制器中的代理方法
- (void)delegateSendToNext:(NSString \* )strDelegate{
   self.lblDelegate.text = strDelegate;
}

3.2 代理B–>A
  1. 在B控制器头文件申明协议;
@protocol NextViewControllerDelegate <NSObject>

@optional

- (void)delegateSend:(NSString \*)strDelegate;

@end

  1. 在B控制器定义遵循协议的属性;
@property (nonatomic,assign) id<NextViewControllerDelegate> delegate;

  1. 在B跳回A的点击事件中调用代理方法;
- (void)btnDelegate{
    
    [self.delegate delegateSend:@"iOS"];

    [self.navigationController popViewControllerAnimated:YES];

}

  1. A遵循B的协议;
@interface ViewController ()<NextViewControllerDelegate>

  1. 在A跳转到B的点击事件中使A成为B的代理者;
//代理传值

- (void)btnDelegate{
    NextViewController \*nextVC = [[NextViewController alloc]init];

    nextVC.delegate = self;

    [self.navigationController pushViewController:nextVC animated:YES];

}

  1. 在A控制器实现B协议中的代理方法;
//代理方法

- (void)delegateSend:(NSString \*)strDelegate{
   self.lblDelegate.text = strDelegate;

}

四、通知传值

通知传值的核心部分,就是建立好监听者与发送者在时间上的关系,即一定要先有监听者再去发送通知,掌握好了这一点就可以玩转通知传值。

4.1 通知传值A–>B
  1. B是消息的接收者,要在A发送消息之前先建立好监听者,这时候就需要在B中重写init方法来达到这个目的,然后写收到通知后的操作,最后一定要记得移除监听者,否则会造成内存泄漏;
- (instancetype)init{
   if (self = [superinit]) {
       //创建监听者
       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:@"bobo" object:nil];
    }
    return self;
}

//通知传值
- (void)changeValue:(NSNotification \*)notification{
   NSDictionary \* dict = notification.userInfo;
   self.lblNotification.text = dict[@"key"];
}

//移除监听者
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"bobo" object:nil];
}

  1. 接下来就是在A的点击事件中添加发送消息即可。
// 通知传值
- (void)btnNotification{
   NextViewController \*nextVC = [[NextViewController alloc]init];

   NSNotification \*notification = [[NSNotification alloc]initWithName:@"bobo" object:nil userInfo:@{@"key":@"shq5785"}];

 // 发送通知
 [[NSNotificationCenter defaultCenter] postNotification:notification];
 [self.navigationController pushViewController:nextVC animated:YES];
}

4.2 通知传值B–>A
  1. 由B到A传值就更简单了,因为时间顺序上一定是先有A后有B,所以只要在A的viewDidLoad中建立一个监听者,然后实现监听事件,最后一处监听者就行
//在viewDidLoad中注册监听者
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeValue:) name:@"bobo2" object:nil];

//收到通知后操作

- (void)changeValue:(NSNotification \*)notification{
   NSDictionary \*dict = notification.userInfo;

   self.lblNotification.text = dict[@"key"];

}

//移除监听者

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"bobo2" object:nil];
}

  1. 在B跳转回A的时候发送通知就行了。
- (void)btnNotification{
    NSNotification \* notification = [[NSNotification alloc]initWithName:@"bobo2" object:nil userInfo:@{@"key":@"iOS"}];

    //发送通知

    [[NSNotificationCenter defaultCenter] postNotification:notification];

    [self.navigationController popViewControllerAnimated:YES];

}

五、单例传值

单例,就是从程序开始到程序结束不管中途怎么去实例化都只有一个实例对象,先创建一个单例(本文已Teacher类作为单例示例):

//Teacher.h

#import <Foundation/Foundation.h>

@interface Teacher :NSObject<NSCopying,NSMutableCopying>

@property (nonatomic,strong) NSString \* name;

@property (nonatomic,assign) int age;

+ (instancetype)shareInstance;

@end

//Teacher.m
#import "Teacher.h"

staticTeacher \*teacher = nil;

@implementation Teacher

+ (instancetype)shareInstance{

   static dispatch_once_t onceToken;

   dispatch\_once(&onceToken, ^{
       teacher = [[Teacher alloc]init];//确保对象只被初始化一次
    });
    
    return teacher;

}

+ (instancetype)allocWithZone:(struct_NSZone \*)zone{
   if (teacher ==nil) {
       teacher = [[superallocWithZone:zone]init];
    }

    return teacher;

}

- (instancetype)copyWithZone:(NSZone \*)zone{
    return teacher;
}

- (id)mutableCopyWithZone:(nullableNSZone \*)zone{
    return teacher;
}

@end

5.1 单例传值A–>B
  1. 单例传值只要在A中拿到单例对象,给对象属性赋值,都在跳转事件执行;
//单例传值

- (void)btnInstance{
    NextViewController \*nextVC = [[NextViewController alloc]init];

   Teacher \*teacher = [Teacher new];

    teacher.name = @"SHQ5785";

    [self.navigationController pushViewController:nextVC animated:YES];

}

  1. 在B界面拿到单例对象在给label赋值就行,在viewDidLoad写如下代码即可:
//单例传值

Teacher \*teacher = [Teacher new];

self.lblInstance.text = teacher.name;

5.2 单例传值B–>A

分享

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值